Lines Matching +full:vm +full:- +full:active +full:- +full:channels

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
16 * Ben-Ami Yassour <benami@il.ibm.com>
48 #include <linux/user-return-notifier.h>
62 #include <linux/entry-kvm.h>
108 ((struct kvm_vcpu *)(ctxt)->vcpu)
111 * - enable syscall per default because its emulated by KVM
112 * - enable LME and LMA per default on 64 bit KVM
143 *(((struct kvm_x86_ops *)0)->func));
146 #include <asm/kvm-x86-ops.h>
163 /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
175 * Flags to manipulate forced emulation behavior (any non-zero value will
182 int __read_mostly pi_inject_timer = -1;
234 STATS_DESC_COUNTER(VM, mmu_shadow_zapped),
235 STATS_DESC_COUNTER(VM, mmu_pte_write),
236 STATS_DESC_COUNTER(VM, mmu_pde_zapped),
237 STATS_DESC_COUNTER(VM, mmu_flooded),
238 STATS_DESC_COUNTER(VM, mmu_recycled),
239 STATS_DESC_COUNTER(VM, mmu_cache_miss),
240 STATS_DESC_ICOUNTER(VM, mmu_unsync),
241 STATS_DESC_ICOUNTER(VM, pages_4k),
242 STATS_DESC_ICOUNTER(VM, pages_2m),
243 STATS_DESC_ICOUNTER(VM, pages_1g),
244 STATS_DESC_ICOUNTER(VM, nx_lpage_splits),
245 STATS_DESC_PCOUNTER(VM, max_mmu_rmap_size),
246 STATS_DESC_PCOUNTER(VM, max_mmu_page_hash_collisions)
445 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
458 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
550 size - useroffset, NULL); in kvm_alloc_emulator_cache()
559 vcpu->arch.apf.gfns[i] = ~0; in kvm_async_pf_hash_reset()
575 if (msrs->registered) { in kvm_on_user_return()
576 msrs->registered = false; in kvm_on_user_return()
581 values = &msrs->values[slot]; in kvm_on_user_return()
582 if (values->host != values->curr) { in kvm_on_user_return()
583 wrmsrq(kvm_uret_msrs_list[slot], values->host); in kvm_on_user_return()
584 values->curr = values->host; in kvm_on_user_return()
609 return -1; in kvm_add_user_return_msr()
624 return -1; in kvm_find_user_return_msr()
636 msrs->values[i].host = value; in kvm_user_return_msr_cpu_online()
637 msrs->values[i].curr = value; in kvm_user_return_msr_cpu_online()
643 if (!msrs->registered) { in kvm_user_return_register_notifier()
644 msrs->urn.on_user_return = kvm_on_user_return; in kvm_user_return_register_notifier()
645 user_return_notifier_register(&msrs->urn); in kvm_user_return_register_notifier()
646 msrs->registered = true; in kvm_user_return_register_notifier()
655 value = (value & mask) | (msrs->values[slot].host & ~mask); in kvm_set_user_return_msr()
656 if (value == msrs->values[slot].curr) in kvm_set_user_return_msr()
662 msrs->values[slot].curr = value; in kvm_set_user_return_msr()
672 msrs->values[slot].curr = value; in kvm_user_return_msr_update_cache()
681 if (msrs->registered) in drop_user_return_notifiers()
682 kvm_on_user_return(&msrs->urn); in drop_user_return_notifiers()
736 * #DBs can be trap-like or fault-like, the caller must check other CPU in exception_type()
755 if (!ex->has_payload) in kvm_deliver_exception_payload()
758 switch (ex->vector) { in kvm_deliver_exception_payload()
761 * "Certain debug exceptions may clear bit 0-3. The in kvm_deliver_exception_payload()
765 vcpu->arch.dr6 &= ~DR_TRAP_BITS; in kvm_deliver_exception_payload()
768 * dr6, three components need to be considered: active low in kvm_deliver_exception_payload()
769 * bit, FIXED_1 bits and active high bits (e.g. DR6_BD, in kvm_deliver_exception_payload()
771 * DR6_ACTIVE_LOW contains the FIXED_1 and active low bits. in kvm_deliver_exception_payload()
774 * Active low bits should be cleared if 1-setting in payload. in kvm_deliver_exception_payload()
775 * Active high bits should be set if 1-setting in payload. in kvm_deliver_exception_payload()
779 * are active high in payload. in kvm_deliver_exception_payload()
782 vcpu->arch.dr6 |= DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
783 vcpu->arch.dr6 |= ex->payload; in kvm_deliver_exception_payload()
784 vcpu->arch.dr6 ^= ex->payload & DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
792 vcpu->arch.dr6 &= ~BIT(12); in kvm_deliver_exception_payload()
795 vcpu->arch.cr2 = ex->payload; in kvm_deliver_exception_payload()
799 ex->has_payload = false; in kvm_deliver_exception_payload()
800 ex->payload = 0; in kvm_deliver_exception_payload()
808 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; in kvm_queue_exception_vmexit()
810 ex->vector = vector; in kvm_queue_exception_vmexit()
811 ex->injected = false; in kvm_queue_exception_vmexit()
812 ex->pending = true; in kvm_queue_exception_vmexit()
813 ex->has_error_code = has_error_code; in kvm_queue_exception_vmexit()
814 ex->error_code = error_code; in kvm_queue_exception_vmexit()
815 ex->has_payload = has_payload; in kvm_queue_exception_vmexit()
816 ex->payload = payload; in kvm_queue_exception_vmexit()
829 * If the exception is destined for L2, morph it to a VM-Exit if L1 in kvm_multiple_exception()
833 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, nr, error_code)) { in kvm_multiple_exception()
839 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { in kvm_multiple_exception()
841 vcpu->arch.exception.pending = true; in kvm_multiple_exception()
842 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
844 vcpu->arch.exception.has_error_code = has_error; in kvm_multiple_exception()
845 vcpu->arch.exception.vector = nr; in kvm_multiple_exception()
846 vcpu->arch.exception.error_code = error_code; in kvm_multiple_exception()
847 vcpu->arch.exception.has_payload = has_payload; in kvm_multiple_exception()
848 vcpu->arch.exception.payload = payload; in kvm_multiple_exception()
851 &vcpu->arch.exception); in kvm_multiple_exception()
856 prev_nr = vcpu->arch.exception.vector; in kvm_multiple_exception()
858 /* triple fault -> shutdown */ in kvm_multiple_exception()
870 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
871 vcpu->arch.exception.pending = false; in kvm_multiple_exception()
876 that instruction re-execution will regenerate lost in kvm_multiple_exception()
907 * On VM-Entry, an exception can be pending if and only if event in kvm_requeue_exception()
917 * re-checking is incorrect if _L1_ injected the exception, in which in kvm_requeue_exception()
922 vcpu->arch.exception.injected = true; in kvm_requeue_exception()
923 vcpu->arch.exception.has_error_code = has_error_code; in kvm_requeue_exception()
924 vcpu->arch.exception.vector = nr; in kvm_requeue_exception()
925 vcpu->arch.exception.error_code = error_code; in kvm_requeue_exception()
926 vcpu->arch.exception.has_payload = false; in kvm_requeue_exception()
927 vcpu->arch.exception.payload = 0; in kvm_requeue_exception()
955 ++vcpu->stat.pf_guest; in kvm_inject_page_fault()
958 * Async #PF in L2 is always forwarded to L1 as a VM-Exit regardless of in kvm_inject_page_fault()
961 if (is_guest_mode(vcpu) && fault->async_page_fault) in kvm_inject_page_fault()
963 true, fault->error_code, in kvm_inject_page_fault()
964 true, fault->address); in kvm_inject_page_fault()
966 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, in kvm_inject_page_fault()
967 fault->address); in kvm_inject_page_fault()
974 WARN_ON_ONCE(fault->vector != PF_VECTOR); in kvm_inject_emulated_page_fault()
976 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : in kvm_inject_emulated_page_fault()
977 vcpu->arch.walk_mmu; in kvm_inject_emulated_page_fault()
983 if ((fault->error_code & PFERR_PRESENT_MASK) && in kvm_inject_emulated_page_fault()
984 !(fault->error_code & PFERR_RSVD_MASK)) in kvm_inject_emulated_page_fault()
985 kvm_mmu_invalidate_addr(vcpu, fault_mmu, fault->address, in kvm_inject_emulated_page_fault()
988 fault_mmu->inject_page_fault(vcpu, fault); in kvm_inject_emulated_page_fault()
994 atomic_inc(&vcpu->arch.nmi_queued); in kvm_inject_nmi()
1028 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); in pdptr_rsvd_bits()
1036 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in load_pdptrs()
1041 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; in load_pdptrs()
1069 if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) in load_pdptrs()
1070 kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); in load_pdptrs()
1072 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); in load_pdptrs()
1075 vcpu->arch.pdptrs_from_userspace = false; in load_pdptrs()
1100 * CR0.WP is incorporated into the MMU role, but only for non-nested, in kvm_post_set_cr0()
1147 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && in kvm_set_cr0()
1158 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && in kvm_set_cr0()
1183 if (vcpu->arch.guest_state_protected) in kvm_load_guest_xsave_state()
1188 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_guest_xsave_state()
1189 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); in kvm_load_guest_xsave_state()
1192 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_guest_xsave_state()
1193 wrmsrq(MSR_IA32_XSS, vcpu->arch.ia32_xss); in kvm_load_guest_xsave_state()
1197 vcpu->arch.pkru != vcpu->arch.host_pkru && in kvm_load_guest_xsave_state()
1198 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_guest_xsave_state()
1200 wrpkru(vcpu->arch.pkru); in kvm_load_guest_xsave_state()
1206 if (vcpu->arch.guest_state_protected) in kvm_load_host_xsave_state()
1210 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_host_xsave_state()
1212 vcpu->arch.pkru = rdpkru(); in kvm_load_host_xsave_state()
1213 if (vcpu->arch.pkru != vcpu->arch.host_pkru) in kvm_load_host_xsave_state()
1214 wrpkru(vcpu->arch.host_pkru); in kvm_load_host_xsave_state()
1219 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_host_xsave_state()
1223 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_host_xsave_state()
1233 return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; in kvm_guest_supported_xfd()
1240 u64 old_xcr0 = vcpu->arch.xcr0; in __kvm_set_xcr()
1256 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; in __kvm_set_xcr()
1275 vcpu->arch.xcr0 = xcr0; in __kvm_set_xcr()
1278 vcpu->arch.cpuid_dynamic_bits_dirty = true; in __kvm_set_xcr()
1307 * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB in kvm_post_set_cr4()
1321 * - CR4.PCIDE is changed from 1 to 0 in kvm_post_set_cr4()
1322 * - CR4.PGE is toggled in kvm_post_set_cr4()
1333 * - CR4.SMEP is changed from 0 to 1 in kvm_post_set_cr4()
1334 * - CR4.PAE is toggled in kvm_post_set_cr4()
1376 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_invalidate_pcid()
1411 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) in kvm_invalidate_pcid()
1414 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); in kvm_invalidate_pcid()
1447 vcpu->arch.cr3 = cr3; in kvm_set_cr3()
1456 * and it's impossible to use a non-zero PCID when PCID is disabled, in kvm_set_cr3()
1473 vcpu->arch.cr8 = cr8; in kvm_set_cr8()
1483 return vcpu->arch.cr8; in kvm_get_cr8()
1491 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { in kvm_update_dr0123()
1493 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_update_dr0123()
1501 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) in kvm_update_dr7()
1502 dr7 = vcpu->arch.guest_debug_dr7; in kvm_update_dr7()
1504 dr7 = vcpu->arch.dr7; in kvm_update_dr7()
1506 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1508 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1526 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_set_dr()
1530 vcpu->arch.db[array_index_nospec(dr, size)] = val; in kvm_set_dr()
1531 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) in kvm_set_dr()
1532 vcpu->arch.eff_db[dr] = val; in kvm_set_dr()
1538 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); in kvm_set_dr()
1544 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; in kvm_set_dr()
1555 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_get_dr()
1559 return vcpu->arch.db[array_index_nospec(dr, size)]; in kvm_get_dr()
1562 return vcpu->arch.dr6; in kvm_get_dr()
1565 return vcpu->arch.dr7; in kvm_get_dr()
1589 * 10 - MISC_PACKAGE_CTRLS
1590 * 11 - ENERGY_FILTERING_CTL
1591 * 12 - DOITM
1592 * 18 - FB_CLEAR_CTRL
1593 * 21 - XAPIC_DISABLE_STATUS
1594 * 23 - OVERCLOCKING_STATUS
1727 u64 old_efer = vcpu->arch.efer; in set_efer()
1728 u64 efer = msr_info->data; in set_efer()
1734 if (!msr_info->host_initiated) { in set_efer()
1739 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1744 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1772 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1781 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1783 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1789 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1790 ranges = msr_filter->ranges; in kvm_msr_allowed()
1792 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1799 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1805 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1814 * Returns 0 on success, non-0 otherwise.
1835 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1836 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1837 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1839 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1841 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1843 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1862 * clear the bits. This ensures cross-vendor migration will in __kvm_set_msr()
1895 * Returns 0 on success, non-0 otherwise.
1962 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1963 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1964 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
1970 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
1981 return kvm_x86_call(complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
2010 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2013 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2014 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2015 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2016 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2017 vcpu->run->msr.index = index; in kvm_msr_user_space()
2018 vcpu->run->msr.data = data; in kvm_msr_user_space()
2019 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2035 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2036 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2098 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS)) in kvm_emulate_monitor_mwait()
2101 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) in kvm_emulate_monitor_mwait()
2104 enabled = vcpu->arch.ia32_misc_enable_msr & MSR_IA32_MISC_ENABLE_MWAIT; in kvm_emulate_monitor_mwait()
2129 return READ_ONCE(vcpu->mode) == EXITING_GUEST_MODE || in kvm_vcpu_exit_request()
2135 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2142 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2149 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2223 return -EINVAL; in do_set_msr()
2255 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2258 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2259 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2260 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2261 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2262 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2263 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2264 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2266 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2267 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2268 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2269 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2270 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2271 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2272 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2274 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2276 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2278 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2338 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2340 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2341 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2344 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2347 vcpu->arch.time = system_time; in kvm_write_system_time()
2352 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2355 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2378 shift--; in kvm_get_time_scale()
2423 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2424 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2428 return -1; in set_tsc_khz()
2432 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2437 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2439 return -1; in set_tsc_khz()
2455 return -1; in kvm_set_tsc_khz()
2460 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2461 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2462 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2470 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2482 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2483 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2484 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2485 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2499 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2507 bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2508 atomic_read(&vcpu->kvm->online_vcpus)) && in kvm_track_tsc_matching()
2509 gtod_is_based_on_tsc(gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2517 if ((ka->use_master_clock && new_generation) || in kvm_track_tsc_matching()
2518 (ka->use_master_clock != use_master_clock)) in kvm_track_tsc_matching()
2521 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, in kvm_track_tsc_matching()
2522 atomic_read(&vcpu->kvm->online_vcpus), in kvm_track_tsc_matching()
2523 ka->use_master_clock, gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2530 * The most significant 64-N bits (mult) of ratio represent the
2533 * point number (mult + frac * 2^(-N)).
2556 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); in kvm_compute_l1_tsc_offset()
2558 return target_tsc - tsc; in kvm_compute_l1_tsc_offset()
2563 return vcpu->arch.l1_tsc_offset + in kvm_read_l1_tsc()
2564 kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); in kvm_read_l1_tsc()
2595 if (vcpu->arch.guest_tsc_protected) in kvm_vcpu_write_tsc_offset()
2598 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in kvm_vcpu_write_tsc_offset()
2599 vcpu->arch.l1_tsc_offset, in kvm_vcpu_write_tsc_offset()
2602 vcpu->arch.l1_tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2610 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( in kvm_vcpu_write_tsc_offset()
2615 vcpu->arch.tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2622 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2624 /* Userspace is changing the multiplier while L2 is active */ in kvm_vcpu_write_tsc_multiplier()
2626 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( in kvm_vcpu_write_tsc_multiplier()
2630 vcpu->arch.tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2640 * TSC is marked unstable when we're running on Hyper-V, in kvm_check_tsc_unstable()
2657 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2659 lockdep_assert_held(&kvm->arch.tsc_write_lock); in __kvm_synchronize_tsc()
2661 if (vcpu->arch.guest_tsc_protected) in __kvm_synchronize_tsc()
2665 vcpu->kvm->arch.user_set_tsc = true; in __kvm_synchronize_tsc()
2671 kvm->arch.last_tsc_nsec = ns; in __kvm_synchronize_tsc()
2672 kvm->arch.last_tsc_write = tsc; in __kvm_synchronize_tsc()
2673 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; in __kvm_synchronize_tsc()
2674 kvm->arch.last_tsc_offset = offset; in __kvm_synchronize_tsc()
2676 vcpu->arch.last_guest_tsc = tsc; in __kvm_synchronize_tsc()
2688 * These values are tracked in kvm->arch.cur_xxx variables. in __kvm_synchronize_tsc()
2690 kvm->arch.cur_tsc_generation++; in __kvm_synchronize_tsc()
2691 kvm->arch.cur_tsc_nsec = ns; in __kvm_synchronize_tsc()
2692 kvm->arch.cur_tsc_write = tsc; in __kvm_synchronize_tsc()
2693 kvm->arch.cur_tsc_offset = offset; in __kvm_synchronize_tsc()
2694 kvm->arch.nr_vcpus_matched_tsc = 0; in __kvm_synchronize_tsc()
2695 } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { in __kvm_synchronize_tsc()
2696 kvm->arch.nr_vcpus_matched_tsc++; in __kvm_synchronize_tsc()
2700 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; in __kvm_synchronize_tsc()
2701 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; in __kvm_synchronize_tsc()
2702 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; in __kvm_synchronize_tsc()
2710 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2716 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2719 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2721 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2728 } else if (kvm->arch.user_set_tsc) { in kvm_synchronize_tsc()
2729 u64 tsc_exp = kvm->arch.last_tsc_write + in kvm_synchronize_tsc()
2731 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; in kvm_synchronize_tsc()
2733 * Here lies UAPI baggage: when a user-initiated TSC write has in kvm_synchronize_tsc()
2744 * come from the kernel's default vCPU creation. Make the 1-second in kvm_synchronize_tsc()
2760 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2762 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2772 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2778 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2784 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2787 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2819 switch (clock->vclock_mode) { in vgettsc()
2825 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2826 clock->mask; in vgettsc()
2835 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2836 clock->mask; in vgettsc()
2845 return v * clock->mult; in vgettsc()
2850 * frequency of CLOCK_MONOTONIC_RAW (hence adding gtos->offs_boot).
2860 seq = read_seqcount_begin(&gtod->seq); in do_kvmclock_base()
2861 ns = gtod->raw_clock.base_cycles; in do_kvmclock_base()
2862 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode); in do_kvmclock_base()
2863 ns >>= gtod->raw_clock.shift; in do_kvmclock_base()
2864 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_kvmclock_base()
2865 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_kvmclock_base()
2883 seq = read_seqcount_begin(&gtod->seq); in do_monotonic()
2884 ns = gtod->clock.base_cycles; in do_monotonic()
2885 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_monotonic()
2886 ns >>= gtod->clock.shift; in do_monotonic()
2887 ns += ktime_to_ns(gtod->clock.offset); in do_monotonic()
2888 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_monotonic()
2902 seq = read_seqcount_begin(&gtod->seq); in do_realtime()
2903 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2904 ns = gtod->clock.base_cycles; in do_realtime()
2905 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_realtime()
2906 ns >>= gtod->clock.shift; in do_realtime()
2907 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_realtime()
2909 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2910 ts->tv_nsec = ns; in do_realtime()
2978 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2979 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2980 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2984 * - ret0 < ret1
2985 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2987 * - 0 < N - M => M < N
3006 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
3010 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
3011 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
3012 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
3019 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
3020 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
3022 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
3023 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
3024 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
3026 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
3030 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
3042 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
3043 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3056 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3060 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3061 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3080 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3094 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3097 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3103 data->flags = 0; in __get_kvmclock()
3104 if (ka->use_master_clock && in __get_kvmclock()
3109 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3110 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3111 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3114 data->host_tsc = rdtsc(); in __get_kvmclock()
3116 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3117 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3118 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3122 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3124 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3132 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3136 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3138 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3160 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3162 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3167 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3170 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3179 guest_hv_clock->version = hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3183 hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3189 guest_hv_clock->version = ++hv_clock.version; in kvm_setup_guest_pvclock()
3192 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3194 trace_kvm_pvclock_update(vcpu->vcpu_id, &hv_clock); in kvm_setup_guest_pvclock()
3202 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3203 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3216 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3217 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3219 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3220 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3222 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3249 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3252 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3263 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3267 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3269 &vcpu->pvclock_tsc_shift, in kvm_guest_time_update()
3270 &vcpu->pvclock_tsc_mul); in kvm_guest_time_update()
3271 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3274 hv_clock.tsc_shift = vcpu->pvclock_tsc_shift; in kvm_guest_time_update()
3275 hv_clock.tsc_to_system_mul = vcpu->pvclock_tsc_mul; in kvm_guest_time_update()
3277 hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3278 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3285 if (vcpu->pv_time.active) { in kvm_guest_time_update()
3289 * is active/enabled. in kvm_guest_time_update()
3291 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_guest_time_update()
3293 vcpu->pvclock_set_guest_stopped_request = false; in kvm_guest_time_update()
3295 kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->pv_time, 0); in kvm_guest_time_update()
3300 kvm_hv_setup_tsc_page(v->kvm, &hv_clock); in kvm_guest_time_update()
3311 if (ka->xen.hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE) in kvm_guest_time_update()
3314 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3315 kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3317 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3318 kvm_setup_guest_pvclock(&hv_clock, v, &vcpu->xen.vcpu_time_info_cache, 0); in kvm_guest_time_update()
3346 struct kvm_arch *ka = &kvm->arch; in kvm_get_wall_clock_epoch()
3352 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_get_wall_clock_epoch()
3355 if (!ka->use_master_clock) in kvm_get_wall_clock_epoch()
3377 hv_clock.tsc_timestamp = ka->master_cycle_now; in kvm_get_wall_clock_epoch()
3378 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in kvm_get_wall_clock_epoch()
3380 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_get_wall_clock_epoch()
3386 * since 1970-01-01. in kvm_get_wall_clock_epoch()
3392 return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - in kvm_get_wall_clock_epoch()
3396 return ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_get_wall_clock_epoch()
3401 * vcpu->cpu migration, should not allow system_timestamp from
3407 * We need to rate-limit these requests though, as they can
3410 * by the delay we use to rate-limit the updates.
3432 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3435 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3448 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3449 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3470 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3477 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3479 u32 msr = msr_info->index; in set_msr_mce()
3480 u64 data = msr_info->data; in set_msr_mce()
3485 vcpu->arch.mcg_status = data; in set_msr_mce()
3489 (data || !msr_info->host_initiated)) in set_msr_mce()
3493 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3495 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3496 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3500 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3505 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3506 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3507 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3509 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3510 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3522 * single-bit ECC data errors. in set_msr_mce()
3530 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3533 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3537 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3538 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3539 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3551 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3573 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3581 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3585 vcpu->arch.apf.send_always = (data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3586 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3595 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3602 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3604 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3611 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3612 vcpu->arch.time = 0; in kvmclock_reset()
3617 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3626 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3642 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3651 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3659 * prior before nested VM-Enter/VM-Exit.
3673 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3676 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3680 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3685 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3688 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3691 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3693 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3694 gpa != ghc->gpa || in record_steal_time()
3695 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3697 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3699 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3700 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3704 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3711 int err = -EFAULT; in record_steal_time()
3722 "+m" (st->preempted)); in record_steal_time()
3728 vcpu->arch.st.preempted = 0; in record_steal_time()
3730 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3741 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3742 vcpu->arch.st.preempted = 0; in record_steal_time()
3745 unsafe_get_user(version, &st->version, out); in record_steal_time()
3750 unsafe_put_user(version, &st->version, out); in record_steal_time()
3754 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3755 steal += current->sched_info.run_delay - in record_steal_time()
3756 vcpu->arch.st.last_steal; in record_steal_time()
3757 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3758 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3761 unsafe_put_user(version, &st->version, out); in record_steal_time()
3766 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3771 u32 msr = msr_info->index; in kvm_set_msr_common()
3772 u64 data = msr_info->data; in kvm_set_msr_common()
3775 * Do not allow host-initiated writes to trigger the Xen hypercall in kvm_set_msr_common()
3779 if (kvm_xen_is_hypercall_page_msr(vcpu->kvm, msr) && in kvm_set_msr_common()
3780 !msr_info->host_initiated) in kvm_set_msr_common()
3795 if (msr_info->host_initiated) in kvm_set_msr_common()
3796 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3799 if (!msr_info->host_initiated || in kvm_set_msr_common()
3802 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3805 if (!msr_info->host_initiated || in kvm_set_msr_common()
3815 * refresh will bug the VM if called after the vCPU has run. in kvm_set_msr_common()
3817 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3820 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3826 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3854 if (!msr_info->host_initiated && in kvm_set_msr_common()
3881 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3893 vcpu->arch.pat = data; in kvm_set_msr_common()
3899 return kvm_apic_set_base(vcpu, data, msr_info->host_initiated); in kvm_set_msr_common()
3907 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3908 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3915 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3919 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3921 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3931 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3935 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3936 vcpu->arch.cpuid_dynamic_bits_dirty = true; in kvm_set_msr_common()
3938 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3943 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3945 vcpu->arch.smbase = data; in kvm_set_msr_common()
3948 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3951 if (msr_info->host_initiated) { in kvm_set_msr_common()
3953 } else if (!vcpu->arch.guest_tsc_protected) { in kvm_set_msr_common()
3954 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3956 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3960 if (!msr_info->host_initiated && in kvm_set_msr_common()
3970 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3971 vcpu->arch.cpuid_dynamic_bits_dirty = true; in kvm_set_msr_common()
3974 if (!msr_info->host_initiated) in kvm_set_msr_common()
3976 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3982 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3983 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3989 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3990 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3996 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
4002 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
4022 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
4036 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
4057 if (data & (-1ULL << 1)) in kvm_set_msr_common()
4060 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
4065 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4066 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4083 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
4101 msr_info->host_initiated); in kvm_set_msr_common()
4104 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
4112 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
4117 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
4120 if (!msr_info->host_initiated) in kvm_set_msr_common()
4122 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
4129 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
4133 if (!msr_info->host_initiated && in kvm_set_msr_common()
4140 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
4143 if (!msr_info->host_initiated && in kvm_set_msr_common()
4150 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
4166 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4176 data = vcpu->arch.mcg_cap; in get_msr_mce()
4181 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4184 data = vcpu->arch.mcg_status; in get_msr_mce()
4186 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4187 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4193 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4194 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4195 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4197 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4198 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4202 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4203 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4204 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4215 switch (msr_info->index) { in kvm_get_msr_common()
4238 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4245 msr_info->data = 0; in kvm_get_msr_common()
4251 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4253 msr_info->data = 0; in kvm_get_msr_common()
4256 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4261 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4266 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4269 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4278 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4283 if (msr_info->host_initiated) { in kvm_get_msr_common()
4284 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4285 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4287 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4288 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4291 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4295 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4300 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4302 msr_info->data = 3; in kvm_get_msr_common()
4316 msr_info->data = 1 << 24; in kvm_get_msr_common()
4319 msr_info->data = vcpu->arch.apic_base; in kvm_get_msr_common()
4322 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4324 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4327 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4330 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4333 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4335 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4338 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4342 msr_info->data = 1000ULL; in kvm_get_msr_common()
4344 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4347 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4353 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4359 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4365 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4371 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4377 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4383 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4389 msr_info->data = 0; in kvm_get_msr_common()
4395 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4401 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4407 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4414 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4415 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4416 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4417 msr_info->host_initiated); in kvm_get_msr_common()
4419 if (!msr_info->host_initiated && in kvm_get_msr_common()
4422 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4426 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4434 msr_info->data = 0x20000000; in kvm_get_msr_common()
4448 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4449 msr_info->host_initiated); in kvm_get_msr_common()
4462 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4467 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4472 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4475 if (!msr_info->host_initiated && in kvm_get_msr_common()
4476 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4478 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4481 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4484 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4488 if (!msr_info->host_initiated && in kvm_get_msr_common()
4492 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4495 if (!msr_info->host_initiated && in kvm_get_msr_common()
4499 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4503 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4524 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4546 r = -EFAULT; in msr_io()
4550 r = -E2BIG; in msr_io()
4555 entries = memdup_user(user_msrs->entries, size); in msr_io()
4563 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4564 r = -EFAULT; in msr_io()
4599 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4603 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4607 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4622 return kvm && kvm->arch.has_protected_state ? 0 : KVM_SYNC_X86_VALID_FIELDS; in kvm_sync_valid_fields()
4761 r = kvm->max_vcpus; in kvm_vm_ioctl_check_extension()
4783 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4784 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4791 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4836 if (attr->group) { in __kvm_x86_dev_get_attr()
4838 return kvm_x86_call(dev_get_attr)(attr->group, attr->attr, val); in __kvm_x86_dev_get_attr()
4839 return -ENXIO; in __kvm_x86_dev_get_attr()
4842 switch (attr->attr) { in __kvm_x86_dev_get_attr()
4847 return -ENXIO; in __kvm_x86_dev_get_attr()
4853 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_x86_dev_get_attr()
4862 return -EFAULT; in kvm_x86_dev_get_attr()
4886 r = -EFAULT; in kvm_arch_dev_ioctl()
4893 r = -E2BIG; in kvm_arch_dev_ioctl()
4896 r = -EFAULT; in kvm_arch_dev_ioctl()
4897 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4900 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4912 r = -EFAULT; in kvm_arch_dev_ioctl()
4916 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4921 r = -EFAULT; in kvm_arch_dev_ioctl()
4928 r = -EFAULT; in kvm_arch_dev_ioctl()
4939 r = -EFAULT; in kvm_arch_dev_ioctl()
4946 r = -E2BIG; in kvm_arch_dev_ioctl()
4949 r = -EFAULT; in kvm_arch_dev_ioctl()
4950 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4966 r = -EFAULT; in kvm_arch_dev_ioctl()
4974 r = -EFAULT; in kvm_arch_dev_ioctl()
4981 r = -EINVAL; in kvm_arch_dev_ioctl()
4995 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
5004 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_vcpu_load()
5006 if (vcpu->scheduled_out && pmu->version && pmu->event_count) { in kvm_arch_vcpu_load()
5007 pmu->need_cleanup = true; in kvm_arch_vcpu_load()
5014 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
5015 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5016 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
5028 * is handled on the nested VM-Exit path. in kvm_arch_vcpu_load()
5036 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
5039 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
5040 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
5041 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
5045 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
5046 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
5047 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
5053 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
5055 if (!vcpu->arch.guest_tsc_protected) in kvm_arch_vcpu_load()
5056 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
5064 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
5066 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
5068 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5070 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
5078 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
5082 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
5085 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
5089 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
5091 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
5092 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
5096 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
5097 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
5100 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
5104 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
5107 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
5109 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
5110 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
5111 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
5114 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
5115 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
5117 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
5118 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
5120 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
5127 if (vcpu->preempted) { in kvm_arch_vcpu_put()
5129 * Assume protected guests are in-kernel. Inefficient yielding in kvm_arch_vcpu_put()
5133 vcpu->arch.preempted_in_kernel = vcpu->arch.guest_state_protected || in kvm_arch_vcpu_put()
5140 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
5141 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
5145 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
5149 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
5155 if (vcpu->arch.apic->guest_apic_protected) in kvm_vcpu_ioctl_get_lapic()
5156 return -EINVAL; in kvm_vcpu_ioctl_get_lapic()
5168 if (vcpu->arch.apic->guest_apic_protected) in kvm_vcpu_ioctl_set_lapic()
5169 return -EINVAL; in kvm_vcpu_ioctl_set_lapic()
5202 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
5213 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
5214 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
5216 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
5217 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5223 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5224 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5226 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5227 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5229 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5230 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5232 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5247 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5248 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5249 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5259 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5265 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5268 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5271 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5273 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5286 * - none of the bits for Machine Check Exceptions are set
5287 * - both the VAL (valid) and UC (uncorrectable) bits are set
5288 * MCI_STATUS_PCC - Processor Context Corrupted
5289 * MCI_STATUS_S - Signaled as a Machine Check Exception
5290 * MCI_STATUS_AR - Software recoverable Action Required
5294 return !mce->mcg_status && in is_ucna()
5295 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5296 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5297 (mce->status & MCI_STATUS_UC); in is_ucna()
5302 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5304 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5305 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5306 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5307 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5310 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5314 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5322 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5324 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5326 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5327 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5329 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5338 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5339 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5345 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5347 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5348 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5354 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5355 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5356 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5357 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5358 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5363 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5364 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5365 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5366 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5387 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5388 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5391 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5392 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5393 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5394 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5396 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5401 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5406 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5407 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5418 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5419 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5420 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5426 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5427 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5429 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5430 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5431 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5432 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5433 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5435 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5436 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5437 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5438 events->interrupt.shadow = kvm_x86_call(get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5440 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5441 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5442 events->nmi.masked = kvm_x86_call(get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5444 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5447 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5448 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5449 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5450 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5452 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5454 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5457 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5458 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5459 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5460 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5461 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5468 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5474 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5476 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5477 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5478 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5479 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5480 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5482 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5484 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5485 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5488 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5489 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5490 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5493 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5494 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5495 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5496 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5502 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5503 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5506 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5508 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5510 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5512 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5513 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5514 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5515 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5516 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5517 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5518 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5520 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5521 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5522 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5523 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5525 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5527 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5528 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5529 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5530 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5531 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5534 kvm_x86_call(set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5536 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5538 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5540 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5542 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5544 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5547 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5549 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5550 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5551 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5553 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5557 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5558 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5559 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5563 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5564 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5566 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5570 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5571 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5572 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5573 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5589 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_debugregs()
5590 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_debugregs()
5591 return -EINVAL; in kvm_vcpu_ioctl_x86_get_debugregs()
5595 BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.db) != ARRAY_SIZE(dbgregs->db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5596 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_get_debugregs()
5597 dbgregs->db[i] = vcpu->arch.db[i]; in kvm_vcpu_ioctl_x86_get_debugregs()
5599 dbgregs->dr6 = vcpu->arch.dr6; in kvm_vcpu_ioctl_x86_get_debugregs()
5600 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5609 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_debugregs()
5610 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_debugregs()
5611 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5613 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5614 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5616 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5617 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5618 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5619 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5621 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_set_debugregs()
5622 vcpu->arch.db[i] = dbgregs->db[i]; in kvm_vcpu_ioctl_x86_set_debugregs()
5625 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5626 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5648 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5651 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5652 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_get_xsave2()
5654 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5655 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5662 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5663 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5669 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5670 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_set_xsave()
5672 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5673 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5675 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5681 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_xcrs()
5682 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_xcrs()
5683 return -EINVAL; in kvm_vcpu_ioctl_x86_get_xcrs()
5686 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5690 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5691 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5692 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5693 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5702 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_xcrs()
5703 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_xcrs()
5704 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5707 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5709 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5710 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5712 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5714 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5716 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5720 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5732 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5733 return -EINVAL; in kvm_set_guest_paused()
5734 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5744 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5749 r = -ENXIO; in kvm_arch_tsc_has_attr()
5758 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_get_attr()
5761 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5763 r = -EFAULT; in kvm_arch_tsc_get_attr()
5764 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5769 r = -ENXIO; in kvm_arch_tsc_get_attr()
5778 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_set_attr()
5779 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5782 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5788 r = -EFAULT; in kvm_arch_tsc_set_attr()
5792 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5794 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5795 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5796 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5798 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5802 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5808 r = -ENXIO; in kvm_arch_tsc_set_attr()
5822 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5825 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5845 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5846 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5848 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5851 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5852 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5856 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5857 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5858 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5866 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5867 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5868 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5870 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5873 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5879 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5884 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5888 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5891 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5898 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5914 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5919 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5925 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5932 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5947 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5965 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5968 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5975 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5979 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5986 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5990 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5993 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6000 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6002 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6006 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6008 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6014 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6020 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6030 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6033 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6036 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6038 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6044 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6053 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6064 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6073 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6089 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6099 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6108 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6109 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
6113 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6121 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6128 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6141 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6144 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6152 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6162 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6170 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6190 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6192 if (vcpu->arch.guest_tsc_protected) in kvm_arch_vcpu_ioctl()
6210 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
6220 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6230 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6231 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
6234 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
6235 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6236 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6239 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
6245 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6246 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6248 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6260 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6261 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6264 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6268 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6283 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6284 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6285 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6297 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6302 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6308 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6316 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6317 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6318 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6322 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6326 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6333 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6334 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6335 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6353 r = -ENOTTY; in kvm_arch_vcpu_ioctl()
6359 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6377 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6378 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6393 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6395 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6398 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6400 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6406 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6410 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6412 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6416 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6420 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6423 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6431 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6435 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6437 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6438 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6440 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6443 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6444 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6446 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6449 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6452 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6461 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6463 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6465 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6466 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6467 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6474 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6476 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6477 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6479 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6480 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6486 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6487 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6488 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6489 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6490 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6491 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6500 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6502 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6503 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6504 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6507 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6508 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6509 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6511 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6513 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6520 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6522 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6526 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6527 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6528 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6539 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6540 * VM-Exit. in kvm_arch_sync_dirty_log()
6545 if (!kvm->arch.cpu_dirty_log_size) in kvm_arch_sync_dirty_log()
6556 return -ENXIO; in kvm_vm_ioctl_irq_line()
6558 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6559 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6569 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6570 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6572 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6574 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6575 if (cap->args[0] & ~kvm_caps.supported_quirks) in kvm_vm_ioctl_enable_cap()
6579 kvm->arch.disabled_quirks |= cap->args[0] & kvm_caps.supported_quirks; in kvm_vm_ioctl_enable_cap()
6583 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6584 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6585 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6587 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6590 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6594 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6595 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6599 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6603 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6604 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6607 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6608 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6609 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6610 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6615 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6616 if (cap->args[0] & ~kvm_get_allowed_disable_exits()) in kvm_vm_ioctl_enable_cap()
6619 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6620 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6623 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6628 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6631 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6632 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6633 if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) in kvm_vm_ioctl_enable_cap()
6634 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6635 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6636 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6637 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6638 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6641 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6644 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6648 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6652 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6656 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6657 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6659 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6663 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6664 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6667 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6668 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6672 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6673 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6680 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6687 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6689 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6694 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6698 r = kvm_x86_call(vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6701 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6705 r = kvm_x86_call(vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6708 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6709 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6712 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6716 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6717 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6719 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6723 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6724 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6727 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6728 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6729 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6732 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6735 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6736 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6739 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6740 if (kvm->arch.bsp_vcpu_id > cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6742 } else if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6744 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6745 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6748 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6751 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6752 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6756 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6758 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6759 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6760 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6761 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6764 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6767 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6781 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6785 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6788 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6789 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6790 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6793 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6796 u64 bus_cycle_ns = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6803 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6809 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6811 r = -ENXIO; in kvm_vm_ioctl_enable_cap()
6812 else if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6813 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6815 kvm->arch.apic_bus_cycle_ns = bus_cycle_ns; in kvm_vm_ioctl_enable_cap()
6816 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6820 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6834 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6845 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6846 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6857 if (!user_range->nmsrs) in kvm_add_msr_filter()
6860 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6861 return -EINVAL; in kvm_add_msr_filter()
6863 if (!user_range->flags) in kvm_add_msr_filter()
6864 return -EINVAL; in kvm_add_msr_filter()
6866 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6868 return -EINVAL; in kvm_add_msr_filter()
6870 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6874 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6875 .flags = user_range->flags, in kvm_add_msr_filter()
6876 .base = user_range->base, in kvm_add_msr_filter()
6877 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6881 msr_filter->count++; in kvm_add_msr_filter()
6894 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6895 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6897 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6898 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6900 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6902 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6906 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6908 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6909 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6916 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6917 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6918 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6919 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6920 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6949 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6950 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6961 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6969 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6970 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6971 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6972 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
7019 return -EFAULT; in kvm_vm_ioctl_get_clock()
7026 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
7031 return -EFAULT; in kvm_vm_ioctl_set_clock()
7038 return -EINVAL; in kvm_vm_ioctl_set_clock()
7058 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
7061 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
7062 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
7065 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
7072 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
7074 int r = -ENOTTY; in kvm_arch_vm_ioctl()
7076 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
7093 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7094 r = -EINVAL; in kvm_arch_vm_ioctl()
7095 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7097 r = -EFAULT; in kvm_arch_vm_ioctl()
7102 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7109 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7111 r = -EEXIST; in kvm_arch_vm_ioctl()
7115 r = -EINVAL; in kvm_arch_vm_ioctl()
7116 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7135 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
7137 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
7140 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7147 r = -EFAULT; in kvm_arch_vm_ioctl()
7152 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7153 r = -EEXIST; in kvm_arch_vm_ioctl()
7154 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7156 r = -ENOENT; in kvm_arch_vm_ioctl()
7159 r = -ENOMEM; in kvm_arch_vm_ioctl()
7160 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
7161 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7164 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7176 r = -ENXIO; in kvm_arch_vm_ioctl()
7182 r = -EFAULT; in kvm_arch_vm_ioctl()
7200 r = -ENXIO; in kvm_arch_vm_ioctl()
7209 r = -EFAULT; in kvm_arch_vm_ioctl()
7212 r = -ENXIO; in kvm_arch_vm_ioctl()
7213 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7218 r = -EFAULT; in kvm_arch_vm_ioctl()
7225 r = -EFAULT; in kvm_arch_vm_ioctl()
7228 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7229 r = -ENXIO; in kvm_arch_vm_ioctl()
7230 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7234 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7238 r = -ENXIO; in kvm_arch_vm_ioctl()
7239 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7244 r = -EFAULT; in kvm_arch_vm_ioctl()
7251 r = -EFAULT; in kvm_arch_vm_ioctl()
7254 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7255 r = -ENXIO; in kvm_arch_vm_ioctl()
7256 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7260 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7265 r = -EFAULT; in kvm_arch_vm_ioctl()
7268 r = -ENXIO; in kvm_arch_vm_ioctl()
7269 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7276 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7277 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7278 r = -EBUSY; in kvm_arch_vm_ioctl()
7280 (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) in kvm_arch_vm_ioctl()
7281 r = -EINVAL; in kvm_arch_vm_ioctl()
7283 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
7284 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7289 r = -EFAULT; in kvm_arch_vm_ioctl()
7298 r = -EFAULT; in kvm_arch_vm_ioctl()
7303 r = -EFAULT; in kvm_arch_vm_ioctl()
7309 r = -EFAULT; in kvm_arch_vm_ioctl()
7318 r = -EFAULT; in kvm_arch_vm_ioctl()
7334 r = -EINVAL; in kvm_arch_vm_ioctl()
7344 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7350 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7354 r = -ENOTTY; in kvm_arch_vm_ioctl()
7363 r = -EFAULT; in kvm_arch_vm_ioctl()
7367 r = -ENOTTY; in kvm_arch_vm_ioctl()
7377 r = -EFAULT; in kvm_arch_vm_ioctl()
7381 r = -ENOTTY; in kvm_arch_vm_ioctl()
7392 r = -EFAULT; in kvm_arch_vm_ioctl()
7407 return -EFAULT; in kvm_arch_vm_ioctl()
7413 r = -ENOTTY; in kvm_arch_vm_ioctl()
7473 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7478 MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7479 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7484 MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7485 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7490 MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1: in kvm_probe_msr_to_save()
7491 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7560 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7565 len -= n; in vcpu_mmio_write()
7580 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7587 len -= n; in vcpu_mmio_read()
7609 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7614 /* NPT walks are always user-walks */ in translate_nested_gpa()
7616 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7624 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7627 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7634 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7638 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7646 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7648 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7655 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7660 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7661 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7662 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7674 bytes -= toread; in kvm_read_guest_virt_helper()
7688 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7694 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7699 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7701 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7747 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7752 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7753 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7754 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7765 bytes -= towrite; in kvm_write_guest_virt_helper()
7793 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7853 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7863 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7864 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7865 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7866 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7871 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7874 return -1; in vcpu_mmio_gva_to_gpa()
7905 if (vcpu->mmio_read_completed) { in read_prepare()
7907 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7908 vcpu->mmio_read_completed = 0; in read_prepare()
7943 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7945 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7971 bool write = ops->write; in emulator_read_write_onepage()
7973 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7982 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7983 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7984 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7992 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7998 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
8003 bytes -= handled; in emulator_read_write_onepage()
8006 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
8007 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
8008 frag->gpa = gpa; in emulator_read_write_onepage()
8009 frag->data = val; in emulator_read_write_onepage()
8010 frag->len = bytes; in emulator_read_write_onepage()
8024 if (ops->read_write_prepare && in emulator_read_write()
8025 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
8028 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
8031 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
8034 now = -addr & ~PAGE_MASK; in emulator_read_write()
8041 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
8044 bytes -= now; in emulator_read_write()
8052 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
8055 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
8057 vcpu->mmio_needed = 1; in emulator_read_write()
8058 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
8060 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
8061 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
8062 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
8063 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
8065 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
8105 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
8119 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
8123 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
8181 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
8197 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
8206 vcpu->arch.pio.port = port; in emulator_pio_in_out()
8207 vcpu->arch.pio.in = in; in emulator_pio_in_out()
8208 vcpu->arch.pio.count = count; in emulator_pio_in_out()
8209 vcpu->arch.pio.size = size; in emulator_pio_in_out()
8212 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
8214 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
8216 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
8217 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
8218 vcpu->run->io.size = size; in emulator_pio_in_out()
8219 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
8220 vcpu->run->io.count = count; in emulator_pio_in_out()
8221 vcpu->run->io.port = port; in emulator_pio_in_out()
8237 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
8238 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
8239 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
8240 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
8241 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
8249 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
8297 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8298 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
8301 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8335 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8348 value = vcpu->arch.cr2; in emulator_get_cr()
8377 vcpu->arch.cr2 = val; in emulator_set_cr()
8390 res = -1; in emulator_set_cr()
8451 desc->type = var.type; in emulator_get_segment()
8452 desc->s = var.s; in emulator_get_segment()
8453 desc->dpl = var.dpl; in emulator_get_segment()
8454 desc->p = var.present; in emulator_get_segment()
8455 desc->avl = var.avl; in emulator_get_segment()
8456 desc->l = var.l; in emulator_get_segment()
8457 desc->d = var.db; in emulator_get_segment()
8458 desc->g = var.g; in emulator_get_segment()
8476 if (desc->g) in emulator_set_segment()
8478 var.type = desc->type; in emulator_set_segment()
8479 var.dpl = desc->dpl; in emulator_set_segment()
8480 var.db = desc->d; in emulator_set_segment()
8481 var.s = desc->s; in emulator_set_segment()
8482 var.l = desc->l; in emulator_set_segment()
8483 var.g = desc->g; in emulator_set_segment()
8484 var.avl = desc->avl; in emulator_set_segment()
8485 var.present = desc->p; in emulator_set_segment()
8558 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8566 &ctxt->exception); in emulator_intercept()
8641 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8643 if (!kvm->vm_bugged) in emulator_vm_bugged()
8733 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8735 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8736 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8737 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8738 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8739 ctxt->exception.error_code); in inject_emulated_exception()
8741 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8754 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8755 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8756 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8763 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8768 ctxt->gpa_available = false; in init_emulate_ctxt()
8769 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8770 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8772 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8773 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8774 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8778 ctxt->interruptibility = 0; in init_emulate_ctxt()
8779 ctxt->have_exception = false; in init_emulate_ctxt()
8780 ctxt->exception.vector = -1; in init_emulate_ctxt()
8781 ctxt->perm_ok = false; in init_emulate_ctxt()
8784 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8789 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8794 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8795 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8796 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8802 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8803 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8804 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8812 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8825 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8826 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8838 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8841 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8842 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8844 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8846 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8847 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8848 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8849 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8852 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8853 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8856 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8861 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8863 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8864 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8883 struct kvm_run *run = vcpu->run; in kvm_prepare_event_vectoring_exit()
8890 run->internal.data[ndata++] = info2; in kvm_prepare_event_vectoring_exit()
8891 run->internal.data[ndata++] = reason; in kvm_prepare_event_vectoring_exit()
8892 run->internal.data[ndata++] = info1; in kvm_prepare_event_vectoring_exit()
8893 run->internal.data[ndata++] = gpa; in kvm_prepare_event_vectoring_exit()
8894 run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu; in kvm_prepare_event_vectoring_exit()
8896 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_prepare_event_vectoring_exit()
8897 run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; in kvm_prepare_event_vectoring_exit()
8898 run->internal.ndata = ndata; in kvm_prepare_event_vectoring_exit()
8904 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8906 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8914 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8943 * a SPTE and write-protect the gfn to resolve the !PRESENT fault, and in kvm_unprotect_and_retry_on_failure()
8952 * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the in kvm_unprotect_and_retry_on_failure()
8953 * guest to let the CPU re-execute the instruction in the hope that the in kvm_unprotect_and_retry_on_failure()
8988 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8990 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8991 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8992 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8993 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8994 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
9033 * active, but AMD compatible CPUs do not. in kvm_is_code_breakpoint_inhibited()
9064 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
9065 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
9066 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
9069 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
9070 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
9073 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
9074 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
9075 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
9076 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
9082 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
9086 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
9087 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
9101 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
9103 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9120 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9133 * (and wrong) when emulating on an intercepted fault-like exception[*], as
9143 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
9151 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
9161 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
9187 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
9194 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
9212 if (ctxt->have_exception && in x86_emulate_instruction()
9215 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
9218 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
9219 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
9237 * injecting single-step #DBs. in x86_emulate_instruction()
9240 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
9241 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
9243 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
9250 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9251 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
9252 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
9257 * If emulation was caused by a write-protection #PF on a non-page_table in x86_emulate_instruction()
9269 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
9270 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
9277 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
9280 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
9281 ctxt->gpa_available = true; in x86_emulate_instruction()
9282 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
9286 ctxt->exception.address = 0; in x86_emulate_instruction()
9302 if (ctxt->have_exception) { in x86_emulate_instruction()
9303 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9304 vcpu->mmio_needed = false; in x86_emulate_instruction()
9307 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9308 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9309 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9310 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9313 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9316 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9317 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9319 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9322 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9323 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9334 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9335 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9338 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9340 * of which are fault-like. in x86_emulate_instruction()
9342 if (!ctxt->have_exception || in x86_emulate_instruction()
9343 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9345 if (ctxt->is_branch) in x86_emulate_instruction()
9347 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9348 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9351 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9360 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9363 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9383 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9389 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9391 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.cui_linear_rip))) in complete_fast_pio_out()
9411 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9412 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9416 vcpu->arch.cui_linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9417 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9427 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9429 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.cui_linear_rip))) { in complete_fast_pio_in()
9430 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9435 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9458 vcpu->arch.cui_linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9459 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9490 khz = freq->new; in tsc_khz_changed()
9511 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9579 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9582 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9588 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9611 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9613 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9616 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9644 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9645 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9703 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9716 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9726 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9729 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9747 return -EIO; in kvm_x86_check_processor_compatibility()
9766 return -EEXIST; in kvm_x86_vendor_init()
9776 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9781 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9794 return -EIO; in kvm_x86_vendor_init()
9802 return -ENOMEM; in kvm_x86_vendor_init()
9808 r = -ENOMEM; in kvm_x86_vendor_init()
9832 kvm_init_pmu_capability(ops->pmu_ops); in kvm_x86_vendor_init()
9837 r = ops->hardware_setup(); in kvm_x86_vendor_init()
9859 if (pi_inject_timer == -1) in kvm_x86_vendor_init()
9868 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in kvm_x86_vendor_init()
9952 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9958 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9959 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9962 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9971 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9973 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9982 * @apicid - apicid of vcpu to be kicked.
10002 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
10008 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
10036 set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason, true); in kvm_apicv_init()
10038 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
10046 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
10052 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
10054 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
10055 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
10059 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
10069 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
10077 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
10096 ++vcpu->stat.hypercalls; in ____kvm_emulate_hypercall()
10109 ret = -KVM_EPERM; in ____kvm_emulate_hypercall()
10113 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10123 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in ____kvm_emulate_hypercall()
10136 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in ____kvm_emulate_hypercall()
10148 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10149 if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) in ____kvm_emulate_hypercall()
10154 ret = -KVM_EINVAL; in ____kvm_emulate_hypercall()
10158 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in ____kvm_emulate_hypercall()
10159 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in ____kvm_emulate_hypercall()
10161 * In principle this should have been -KVM_ENOSYS, but userspace (QEMU <=9.2) in ____kvm_emulate_hypercall()
10162 * assumed that vcpu->run->hypercall.ret is never changed by KVM and thus that in ____kvm_emulate_hypercall()
10164 * vcpu->run->hypercall.ret, ensuring that it is zero to not break QEMU. in ____kvm_emulate_hypercall()
10166 vcpu->run->hypercall.ret = 0; in ____kvm_emulate_hypercall()
10167 vcpu->run->hypercall.args[0] = gpa; in ____kvm_emulate_hypercall()
10168 vcpu->run->hypercall.args[1] = npages; in ____kvm_emulate_hypercall()
10169 vcpu->run->hypercall.args[2] = attrs; in ____kvm_emulate_hypercall()
10170 vcpu->run->hypercall.flags = 0; in ____kvm_emulate_hypercall()
10172 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in ____kvm_emulate_hypercall()
10174 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in ____kvm_emulate_hypercall()
10175 vcpu->arch.complete_userspace_io = complete_hypercall; in ____kvm_emulate_hypercall()
10179 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10184 vcpu->run->hypercall.ret = ret; in ____kvm_emulate_hypercall()
10191 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
10212 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
10213 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
10214 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
10215 ctxt->have_exception = true; in emulator_fix_hypercall()
10222 &ctxt->exception); in emulator_fix_hypercall()
10227 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
10228 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
10231 /* Called within kvm->srcu read side. */
10234 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
10236 kvm_run->if_flag = kvm_x86_call(get_if_flag)(vcpu); in post_kvm_run_save()
10237 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
10238 kvm_run->apic_base = vcpu->arch.apic_base; in post_kvm_run_save()
10240 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
10241 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
10245 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
10247 kvm_run->flags |= KVM_RUN_X86_GUEST_MODE; in post_kvm_run_save()
10260 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10263 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10266 max_irr = -1; in update_cr8_intercept()
10268 if (max_irr != -1) in update_cr8_intercept()
10280 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10284 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10293 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10296 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10298 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10299 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10300 vcpu->arch.exception.error_code, in kvm_inject_exception()
10301 vcpu->arch.exception.injected); in kvm_inject_exception()
10311 * injected as part of a previous VM-Enter, but weren't successfully delivered
10312 * and need to be re-injected.
10317 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10318 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10323 * instruction boundaries for asynchronous events. However, because VM-Exits
10329 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10352 * Process nested events first, as nested VM-Exit supersedes event in kvm_check_and_inject_events()
10353 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10354 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10362 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10366 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10375 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10377 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10380 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10384 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10388 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10390 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10394 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10395 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10398 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10399 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10403 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10406 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10407 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10414 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10415 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10416 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10418 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10419 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10423 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10428 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10430 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10431 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10432 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10437 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10439 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10443 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10444 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10445 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10446 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10453 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10454 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10460 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10465 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10466 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10472 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10475 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10477 -EBUSY; in kvm_check_and_inject_events()
10481 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10482 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10490 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10492 -EBUSY; in kvm_check_and_inject_events()
10496 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10497 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10502 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10508 -EBUSY; in kvm_check_and_inject_events()
10514 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10525 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10526 kvm_x86_ops.nested_ops->has_events(vcpu, true)) in kvm_check_and_inject_events()
10531 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10533 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10541 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10542 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10546 if (r == -EBUSY) { in kvm_check_and_inject_events()
10567 if (kvm_x86_call(get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10574 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10577 limit--; in process_nmi()
10579 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10580 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10582 if (vcpu->arch.nmi_pending && in process_nmi()
10584 vcpu->arch.nmi_pending--; in process_nmi()
10586 if (vcpu->arch.nmi_pending) in process_nmi()
10590 /* Return total number of NMIs pending injection to the VM */
10593 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10610 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10616 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10623 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10626 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10633 * still active when the interrupt got accepted. Make sure in __kvm_vcpu_update_apicv()
10636 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10641 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10661 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10673 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10678 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10696 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10699 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10702 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10705 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10715 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10717 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10726 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10727 vcpu->arch.highest_stale_pending_ioapic_eoi = -1; in vcpu_scan_ioapic()
10731 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10732 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10733 else if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10734 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10737 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10744 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10752 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10753 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10759 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10776 * Called within kvm->srcu read side.
10793 r = -EIO; in vcpu_enter_guest()
10803 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10813 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10837 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10838 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10849 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10855 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10858 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10859 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10866 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10883 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10884 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10885 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10886 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10887 vcpu->run->eoi.vector = in vcpu_enter_guest()
10888 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10901 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10902 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10903 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10908 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10909 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10910 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10917 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10918 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10925 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10926 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10943 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { in vcpu_enter_guest()
10952 ++vcpu->stat.req_event; in vcpu_enter_guest()
10958 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10993 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10994 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10999 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
11002 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
11023 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11039 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11040 wrmsrq(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
11042 if (unlikely(vcpu->arch.switch_db_regs && in vcpu_enter_guest()
11043 !(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) { in vcpu_enter_guest()
11045 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
11046 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
11047 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
11048 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
11050 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) in vcpu_enter_guest()
11051 kvm_x86_call(set_dr6)(vcpu, vcpu->arch.dr6); in vcpu_enter_guest()
11056 vcpu->arch.host_debugctl = get_debugctlmsr(); in vcpu_enter_guest()
11062 * Assert that vCPU vs. VM APICv state is consistent. An APICv in vcpu_enter_guest()
11064 * per-VM state, and responding vCPUs must wait for the update in vcpu_enter_guest()
11083 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
11084 ++vcpu->stat.exits; in vcpu_enter_guest()
11093 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
11094 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
11095 WARN_ON(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH); in vcpu_enter_guest()
11104 * If we don't have active breakpoints in the host, we don't in vcpu_enter_guest()
11106 * we have some of them active, restore the old state. in vcpu_enter_guest()
11111 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
11112 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
11114 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11119 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
11122 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
11127 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11132 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
11139 ++vcpu->stat.exits; in vcpu_enter_guest()
11158 * Call this to ensure WC buffers in guest are evicted after each VM in vcpu_enter_guest()
11167 !vcpu->arch.guest_state_protected)) { in vcpu_enter_guest()
11172 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
11175 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
11188 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
11196 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
11197 !vcpu->arch.apf.halted); in kvm_vcpu_running()
11202 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
11213 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
11219 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
11237 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
11238 kvm_x86_ops.nested_ops->has_events(vcpu, false)) in kvm_vcpu_has_events()
11250 return kvm_vcpu_running(vcpu) || vcpu->arch.pv.pv_unhalted || in kvm_arch_vcpu_runnable()
11254 /* Called within kvm->srcu read side. */
11261 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
11264 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
11272 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
11293 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
11299 WARN_ON_ONCE(r == -EBUSY); in vcpu_block()
11306 switch(vcpu->arch.mp_state) { in vcpu_block()
11312 vcpu->arch.apf.halted = false; in vcpu_block()
11323 /* Called within kvm->srcu read side. */
11328 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; in vcpu_run()
11337 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11357 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11358 ++vcpu->stat.request_irq_exits; in vcpu_run()
11378 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
11383 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
11385 if (kvm_vcpu_has_events(vcpu) || vcpu->arch.pv.pv_unhalted) in __kvm_emulate_halt()
11390 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
11405 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
11447 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_preempted_in_kernel()
11452 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
11472 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11497 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11501 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11504 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11505 len = min(8u, frag->len); in complete_emulated_mmio()
11506 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11507 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11509 if (frag->len <= 8) { in complete_emulated_mmio()
11512 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11515 frag->data += len; in complete_emulated_mmio()
11516 frag->gpa += len; in complete_emulated_mmio()
11517 frag->len -= len; in complete_emulated_mmio()
11520 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11521 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11523 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11524 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11526 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11530 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11531 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11532 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11533 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11534 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11535 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11536 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11543 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11544 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11551 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11552 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11558 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11559 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11563 r = kvm_mmu_post_init_vm(vcpu->kvm); in kvm_arch_vcpu_ioctl_run()
11569 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11573 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11574 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11575 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11582 * APIC timer to be active is if userspace stuffed vCPU state, in kvm_arch_vcpu_ioctl_run()
11597 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11599 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11600 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11601 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11606 sync_valid_fields = kvm_sync_valid_fields(vcpu->kvm); in kvm_arch_vcpu_ioctl_run()
11607 if ((kvm_run->kvm_valid_regs & ~sync_valid_fields) || in kvm_arch_vcpu_ioctl_run()
11608 (kvm_run->kvm_dirty_regs & ~sync_valid_fields)) { in kvm_arch_vcpu_ioctl_run()
11609 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11613 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11619 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11621 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11622 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11628 * If userspace set a pending exception and L2 is active, convert it to in kvm_arch_vcpu_ioctl_run()
11629 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11631 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11632 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11633 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11634 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11635 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11636 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11637 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11638 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11640 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11642 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11643 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11644 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11649 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11650 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11653 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11654 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11666 if (kvm_run->kvm_valid_regs && likely(!vcpu->arch.guest_state_protected)) in kvm_arch_vcpu_ioctl_run()
11678 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11686 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11687 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11689 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11690 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11691 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11692 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11693 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11694 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11695 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11696 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11698 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11699 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11700 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11701 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11702 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11703 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11704 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11705 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11708 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11709 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11714 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_regs()
11715 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_regs()
11716 return -EINVAL; in kvm_arch_vcpu_ioctl_get_regs()
11726 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11727 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11729 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11730 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11731 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11732 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11733 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11734 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11735 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11736 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11738 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11739 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11740 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11741 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11742 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11743 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11744 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11745 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11748 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11749 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11751 vcpu->arch.exception.pending = false; in __set_regs()
11752 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11759 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_regs()
11760 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_regs()
11761 return -EINVAL; in kvm_arch_vcpu_ioctl_set_regs()
11773 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11776 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11777 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11778 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11779 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11780 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11781 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11783 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11784 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11787 sregs->idt.limit = dt.size; in __get_sregs_common()
11788 sregs->idt.base = dt.address; in __get_sregs_common()
11790 sregs->gdt.limit = dt.size; in __get_sregs_common()
11791 sregs->gdt.base = dt.address; in __get_sregs_common()
11793 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11794 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11797 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11798 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11799 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11800 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11801 sregs->apic_base = vcpu->arch.apic_base; in __get_sregs_common()
11808 if (vcpu->arch.guest_state_protected) in __get_sregs()
11811 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11812 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11813 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11822 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11827 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11828 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11835 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_sregs()
11836 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_sregs()
11837 return -EINVAL; in kvm_arch_vcpu_ioctl_get_sregs()
11861 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11862 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11863 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11864 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11866 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11880 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11884 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11907 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11908 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11909 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11912 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11914 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11916 kvm_set_mp_state(vcpu, mp_state->mp_state); in kvm_arch_vcpu_ioctl_set_mpstate()
11928 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11940 if (ret || vcpu->mmio_needed) { in kvm_task_switch()
11941 vcpu->mmio_needed = false; in kvm_task_switch()
11942 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11943 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11944 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11948 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11949 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11956 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11959 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11962 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11964 if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11968 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11969 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11971 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11975 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11976 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11986 return -EINVAL; in __set_sregs_common()
11988 if (kvm_apic_set_base(vcpu, sregs->apic_base, true)) in __set_sregs_common()
11989 return -EINVAL; in __set_sregs_common()
11991 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11994 dt.size = sregs->idt.limit; in __set_sregs_common()
11995 dt.address = sregs->idt.base; in __set_sregs_common()
11997 dt.size = sregs->gdt.limit; in __set_sregs_common()
11998 dt.address = sregs->gdt.base; in __set_sregs_common()
12001 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
12002 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
12003 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
12005 kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
12007 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
12009 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
12010 kvm_x86_call(set_efer)(vcpu, sregs->efer); in __set_sregs_common()
12012 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
12013 kvm_x86_call(set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
12015 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
12016 kvm_x86_call(set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
12019 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
12024 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
12027 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
12028 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
12029 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
12030 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
12031 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
12032 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
12034 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
12035 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
12041 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
12064 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
12077 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
12078 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
12079 !(sregs2->efer & EFER_LMA); in __set_sregs2()
12082 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
12083 return -EINVAL; in __set_sregs2()
12085 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
12086 return -EINVAL; in __set_sregs2()
12095 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
12099 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
12113 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_sregs()
12114 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_sregs()
12115 return -EINVAL; in kvm_arch_vcpu_ioctl_set_sregs()
12132 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12135 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12141 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12150 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
12151 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
12155 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12156 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
12159 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
12171 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
12172 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
12173 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
12175 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12177 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12178 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12181 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12185 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
12186 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
12196 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
12211 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
12217 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
12219 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
12220 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
12221 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
12222 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
12223 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
12233 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
12234 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_get_fpu()
12238 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
12239 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
12240 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
12241 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
12242 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
12243 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
12244 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
12245 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
12246 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
12256 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
12257 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_set_fpu()
12261 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
12263 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
12264 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
12265 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
12266 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
12267 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
12268 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
12269 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
12270 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
12280 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
12281 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
12283 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
12284 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
12286 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
12288 vcpu, &vcpu->run->s.regs.events); in store_regs()
12293 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
12294 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
12295 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
12298 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
12299 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
12302 return -EINVAL; in sync_regs()
12304 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
12307 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
12308 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
12311 return -EINVAL; in sync_regs()
12313 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
12321 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
12322 pr_warn_once("SMP vm created on host with unstable TSC; " in kvm_arch_vcpu_precreate()
12325 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12326 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
12328 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12329 return -EINVAL; in kvm_arch_vcpu_precreate()
12339 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
12340 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
12341 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
12343 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm); in kvm_arch_vcpu_create()
12345 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
12358 r = -ENOMEM; in kvm_arch_vcpu_create()
12363 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
12365 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
12367 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
12369 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
12371 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
12373 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
12380 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
12387 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_STUFF_FEATURE_MSRS)) { in kvm_arch_vcpu_create()
12388 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
12389 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
12390 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
12394 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
12395 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
12398 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
12408 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
12415 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
12417 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
12419 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
12421 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
12422 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
12423 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
12433 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
12435 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12442 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12444 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12446 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12447 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12465 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12466 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12467 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12472 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12473 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12475 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12477 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12478 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12479 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12489 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12499 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12500 * possible to INIT the vCPU while L2 is active. Force the vCPU back in kvm_vcpu_reset()
12510 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12512 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12513 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12514 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12515 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12516 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12520 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12522 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12523 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12526 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12529 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12530 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12531 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12537 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12539 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12540 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12557 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12559 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_vcpu_reset()
12561 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12562 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12570 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12581 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12588 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12610 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12680 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_enable_virtualization_cpu()
12682 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_enable_virtualization_cpu()
12684 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_enable_virtualization_cpu()
12685 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_enable_virtualization_cpu()
12715 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_enable_virtualization_cpu()
12729 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_enable_virtualization_cpu()
12731 kvm->arch.backwards_tsc_observed = true; in kvm_arch_enable_virtualization_cpu()
12733 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_enable_virtualization_cpu()
12734 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_enable_virtualization_cpu()
12740 * booting a VM while issuing an S4 host suspend.... in kvm_arch_enable_virtualization_cpu()
12744 kvm->arch.last_tsc_nsec = 0; in kvm_arch_enable_virtualization_cpu()
12745 kvm->arch.last_tsc_write = 0; in kvm_arch_enable_virtualization_cpu()
12760 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12766 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12772 kfree(kvm->arch.hv_pa_pg); in kvm_arch_free_vm()
12784 return -EINVAL; in kvm_arch_init_vm()
12786 kvm->arch.vm_type = type; in kvm_arch_init_vm()
12787 kvm->arch.has_private_mem = in kvm_arch_init_vm()
12789 /* Decided by the vendor code for other VM types. */ in kvm_arch_init_vm()
12790 kvm->arch.pre_fault_allowed = in kvm_arch_init_vm()
12792 kvm->arch.disabled_quirks = kvm_caps.inapplicable_quirks & kvm_caps.supported_quirks; in kvm_arch_init_vm()
12804 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12805 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12808 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12809 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12811 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12813 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12814 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12815 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12816 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12818 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12820 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12822 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12823 kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT; in kvm_arch_init_vm()
12824 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12825 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12828 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12829 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12832 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12833 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12846 once_init(&kvm->arch.nx_once); in kvm_arch_init_vm()
12859 * @kvm: the kvm pointer to the VM.
12869 * -errno: on error
12873 * remain valid and unchanged until the VM is destroyed, i.e., the
12874 * GPA->HVA translation will not change. However, the HVA is a user
12886 lockdep_assert_held(&kvm->slots_lock); in __x86_set_memory_region()
12889 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12893 if (slot && slot->npages) in __x86_set_memory_region()
12894 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12905 if (!slot || !slot->npages) in __x86_set_memory_region()
12908 old_npages = slot->npages; in __x86_set_memory_region()
12909 hva = slot->userspace_addr; in __x86_set_memory_region()
12937 * is unsafe, i.e. will lead to use-after-free. The PIT also needs to in kvm_arch_pre_destroy_vm()
12940 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_pre_destroy_vm()
12941 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_pre_destroy_vm()
12951 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12957 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12963 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12966 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12969 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12970 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12983 vfree(slot->arch.rmap[i]); in memslot_rmap_free()
12984 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12995 vfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12996 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
13004 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
13011 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
13014 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
13015 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
13017 return -ENOMEM; in memslot_rmap_alloc()
13027 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
13035 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
13055 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
13057 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
13059 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
13060 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
13061 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
13066 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
13087 vfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
13088 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
13090 return -ENOMEM; in kvm_alloc_memslot_metadata()
13099 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
13104 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
13116 * trackers attached to the VM, i.e. if KVMGT is in use. in kvm_arch_prepare_memory_region()
13119 return -EINVAL; in kvm_arch_prepare_memory_region()
13122 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
13123 return -EINVAL; in kvm_arch_prepare_memory_region()
13125 if (kvm_is_gfn_alias(kvm, new->base_gfn + new->npages - 1)) in kvm_arch_prepare_memory_region()
13126 return -EINVAL; in kvm_arch_prepare_memory_region()
13132 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
13134 return -EIO; in kvm_arch_prepare_memory_region()
13144 if (!kvm->arch.cpu_dirty_log_size) in kvm_mmu_update_cpu_dirty_logging()
13147 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
13157 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
13158 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
13178 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
13187 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
13200 * Dirty logging might be disabled by userspace if an ongoing VM in kvm_mmu_slot_apply_flags()
13201 * live migration is cancelled and the VM must continue running in kvm_mmu_slot_apply_flags()
13207 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
13216 if (kvm->arch.cpu_dirty_log_size) { in kvm_mmu_slot_apply_flags()
13232 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
13239 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
13248 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
13249 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
13250 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
13251 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
13253 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
13256 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
13257 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
13260 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
13263 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
13277 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
13281 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
13297 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
13307 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_get_ip()
13326 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
13347 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
13355 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
13356 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13377 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13384 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13387 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13396 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13397 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13405 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13414 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13418 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13421 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13423 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13430 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13439 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13447 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13456 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13469 if (!vcpu->arch.apf.send_always && in kvm_can_deliver_async_pf()
13470 (vcpu->arch.guest_state_protected || !kvm_x86_call(get_cpl)(vcpu))) in kvm_can_deliver_async_pf()
13478 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13496 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13511 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13512 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13520 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13543 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13546 if (work->wakeup_all) in kvm_arch_async_page_present()
13547 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13549 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13550 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13552 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13554 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13555 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13559 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13566 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13580 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13587 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13593 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13600 * Non-coherent DMA assignment and de-assignment may affect whether or in kvm_noncoherent_dma_assignment_start_or_stop()
13603 * (or last) non-coherent device is (un)registered to so that new SPTEs in kvm_noncoherent_dma_assignment_start_or_stop()
13614 if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) in kvm_arch_register_noncoherent_dma()
13621 if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) in kvm_arch_unregister_noncoherent_dma()
13628 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13637 struct kvm *kvm = irqfd->kvm; in kvm_arch_irq_bypass_add_producer()
13640 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13642 spin_lock_irq(&kvm->irqfds.lock); in kvm_arch_irq_bypass_add_producer()
13643 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13645 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13646 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13648 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13650 spin_unlock_irq(&kvm->irqfds.lock); in kvm_arch_irq_bypass_add_producer()
13662 struct kvm *kvm = irqfd->kvm; in kvm_arch_irq_bypass_del_producer()
13664 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13668 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13672 spin_lock_irq(&kvm->irqfds.lock); in kvm_arch_irq_bypass_del_producer()
13673 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13675 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_del_producer()
13676 prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13679 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13681 spin_unlock_irq(&kvm->irqfds.lock); in kvm_arch_irq_bypass_del_producer()
13684 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13696 if (old->type != KVM_IRQ_ROUTING_MSI || in kvm_arch_irqfd_route_changed()
13697 new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13700 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13710 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13756 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13762 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13764 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13775 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13788 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13789 return -EIO; in kvm_handle_memory_failure()
13799 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13855 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13874 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13878 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13881 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13882 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13883 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13884 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13886 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13889 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13892 frag->data += len; in complete_sev_es_emulated_mmio()
13893 frag->gpa += len; in complete_sev_es_emulated_mmio()
13894 frag->len -= len; in complete_sev_es_emulated_mmio()
13897 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13898 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13906 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13907 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13908 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13909 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13910 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13911 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13913 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13925 return -EINVAL; in kvm_sev_es_mmio_write()
13931 bytes -= handled; in kvm_sev_es_mmio_write()
13936 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13937 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13938 frag->len = bytes; in kvm_sev_es_mmio_write()
13939 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13940 frag->data = data; in kvm_sev_es_mmio_write()
13942 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13943 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13945 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13946 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13947 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13948 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13949 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13951 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13964 return -EINVAL; in kvm_sev_es_mmio_read()
13970 bytes -= handled; in kvm_sev_es_mmio_read()
13975 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13976 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13977 frag->len = bytes; in kvm_sev_es_mmio_read()
13978 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13979 frag->data = data; in kvm_sev_es_mmio_read()
13981 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13982 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13984 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13985 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13986 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13987 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13989 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13997 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13998 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
14006 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
14007 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
14009 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
14010 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
14020 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
14021 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
14029 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
14033 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
14042 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
14043 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
14044 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
14046 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
14048 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
14058 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
14059 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
14064 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
14068 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
14076 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
14077 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()