Lines Matching +full:acquisition +full:- +full:time +full:- +full:ns

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>
103 ((struct kvm_vcpu *)(ctxt)->vcpu)
106 * - enable syscall per default because its emulated by KVM
107 * - enable LME and LMA per default on 64 bit KVM
140 *(((struct kvm_x86_ops *)0)->func));
143 #include <asm/kvm-x86-ops.h>
160 /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
165 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
166 * adaptive tuning starting from default advancement of 1000ns. '0' disables
167 * advancement entirely. Any other value is used as-is and disables adaptive
168 * tuning, i.e. allows privileged userspace to set an exact advancement time.
170 static int __read_mostly lapic_timer_advance_ns = -1;
181 * Flags to manipulate forced emulation behavior (any non-zero value will
188 int __read_mostly pi_inject_timer = -1;
350 size - useroffset, NULL); in kvm_alloc_emulator_cache()
359 vcpu->arch.apf.gfns[i] = ~0; in kvm_async_pf_hash_reset()
375 if (msrs->registered) { in kvm_on_user_return()
376 msrs->registered = false; in kvm_on_user_return()
381 values = &msrs->values[slot]; in kvm_on_user_return()
382 if (values->host != values->curr) { in kvm_on_user_return()
383 wrmsrl(kvm_uret_msrs_list[slot], values->host); in kvm_on_user_return()
384 values->curr = values->host; in kvm_on_user_return()
409 return -1; in kvm_add_user_return_msr()
424 return -1; in kvm_find_user_return_msr()
437 msrs->values[i].host = value; in kvm_user_return_msr_cpu_online()
438 msrs->values[i].curr = value; in kvm_user_return_msr_cpu_online()
448 value = (value & mask) | (msrs->values[slot].host & ~mask); in kvm_set_user_return_msr()
449 if (value == msrs->values[slot].curr) in kvm_set_user_return_msr()
455 msrs->values[slot].curr = value; in kvm_set_user_return_msr()
456 if (!msrs->registered) { in kvm_set_user_return_msr()
457 msrs->urn.on_user_return = kvm_on_user_return; in kvm_set_user_return_msr()
458 user_return_notifier_register(&msrs->urn); in kvm_set_user_return_msr()
459 msrs->registered = true; in kvm_set_user_return_msr()
470 if (msrs->registered) in drop_user_return_notifiers()
471 kvm_on_user_return(&msrs->urn); in drop_user_return_notifiers()
476 return vcpu->arch.apic_base; in kvm_get_apic_base()
488 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); in kvm_set_apic_base()
492 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) in kvm_set_apic_base()
494 if (!msr_info->host_initiated) { in kvm_set_apic_base()
501 kvm_lapic_set_base(vcpu, msr_info->data); in kvm_set_apic_base()
502 kvm_recalculate_apic_map(vcpu->kvm); in kvm_set_apic_base()
557 * #DBs can be trap-like or fault-like, the caller must check other CPU in exception_type()
576 if (!ex->has_payload) in kvm_deliver_exception_payload()
579 switch (ex->vector) { in kvm_deliver_exception_payload()
582 * "Certain debug exceptions may clear bit 0-3. The in kvm_deliver_exception_payload()
586 vcpu->arch.dr6 &= ~DR_TRAP_BITS; in kvm_deliver_exception_payload()
595 * Active low bits should be cleared if 1-setting in payload. in kvm_deliver_exception_payload()
596 * Active high bits should be set if 1-setting in payload. in kvm_deliver_exception_payload()
603 vcpu->arch.dr6 |= DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
604 vcpu->arch.dr6 |= ex->payload; in kvm_deliver_exception_payload()
605 vcpu->arch.dr6 ^= ex->payload & DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
613 vcpu->arch.dr6 &= ~BIT(12); in kvm_deliver_exception_payload()
616 vcpu->arch.cr2 = ex->payload; in kvm_deliver_exception_payload()
620 ex->has_payload = false; in kvm_deliver_exception_payload()
621 ex->payload = 0; in kvm_deliver_exception_payload()
629 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; in kvm_queue_exception_vmexit()
631 ex->vector = vector; in kvm_queue_exception_vmexit()
632 ex->injected = false; in kvm_queue_exception_vmexit()
633 ex->pending = true; in kvm_queue_exception_vmexit()
634 ex->has_error_code = has_error_code; in kvm_queue_exception_vmexit()
635 ex->error_code = error_code; in kvm_queue_exception_vmexit()
636 ex->has_payload = has_payload; in kvm_queue_exception_vmexit()
637 ex->payload = payload; in kvm_queue_exception_vmexit()
643 kvm_x86_ops.nested_ops->leave_nested(vcpu); in kvm_leave_nested()
657 * morph it to a VM-Exit if L1 wants to intercept the exception. A in kvm_multiple_exception()
659 * when it was original queued, and re-checking is incorrect if _L1_ in kvm_multiple_exception()
663 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, nr, error_code)) { in kvm_multiple_exception()
669 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { in kvm_multiple_exception()
673 * On VM-Entry, an exception can be pending if and only in kvm_multiple_exception()
680 vcpu->arch.exception.injected = true; in kvm_multiple_exception()
690 vcpu->arch.exception.pending = true; in kvm_multiple_exception()
691 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
693 vcpu->arch.exception.has_error_code = has_error; in kvm_multiple_exception()
694 vcpu->arch.exception.vector = nr; in kvm_multiple_exception()
695 vcpu->arch.exception.error_code = error_code; in kvm_multiple_exception()
696 vcpu->arch.exception.has_payload = has_payload; in kvm_multiple_exception()
697 vcpu->arch.exception.payload = payload; in kvm_multiple_exception()
700 &vcpu->arch.exception); in kvm_multiple_exception()
705 prev_nr = vcpu->arch.exception.vector; in kvm_multiple_exception()
707 /* triple fault -> shutdown */ in kvm_multiple_exception()
719 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
720 vcpu->arch.exception.pending = false; in kvm_multiple_exception()
725 that instruction re-execution will regenerate lost in kvm_multiple_exception()
781 ++vcpu->stat.pf_guest; in kvm_inject_page_fault()
784 * Async #PF in L2 is always forwarded to L1 as a VM-Exit regardless of in kvm_inject_page_fault()
787 if (is_guest_mode(vcpu) && fault->async_page_fault) in kvm_inject_page_fault()
789 true, fault->error_code, in kvm_inject_page_fault()
790 true, fault->address); in kvm_inject_page_fault()
792 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, in kvm_inject_page_fault()
793 fault->address); in kvm_inject_page_fault()
800 WARN_ON_ONCE(fault->vector != PF_VECTOR); in kvm_inject_emulated_page_fault()
802 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : in kvm_inject_emulated_page_fault()
803 vcpu->arch.walk_mmu; in kvm_inject_emulated_page_fault()
809 if ((fault->error_code & PFERR_PRESENT_MASK) && in kvm_inject_emulated_page_fault()
810 !(fault->error_code & PFERR_RSVD_MASK)) in kvm_inject_emulated_page_fault()
811 kvm_mmu_invalidate_addr(vcpu, fault_mmu, fault->address, in kvm_inject_emulated_page_fault()
814 fault_mmu->inject_page_fault(vcpu, fault); in kvm_inject_emulated_page_fault()
820 atomic_inc(&vcpu->arch.nmi_queued); in kvm_inject_nmi()
860 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); in pdptr_rsvd_bits()
868 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in load_pdptrs()
873 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; in load_pdptrs()
901 if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) in load_pdptrs()
902 kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); in load_pdptrs()
904 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); in load_pdptrs()
907 vcpu->arch.pdptrs_from_userspace = false; in load_pdptrs()
932 * CR0.WP is incorporated into the MMU role, but only for non-nested, in kvm_post_set_cr0()
965 kvm_arch_has_noncoherent_dma(vcpu->kvm) && in kvm_post_set_cr0()
966 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) in kvm_post_set_cr0()
967 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); in kvm_post_set_cr0()
984 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && in kvm_set_cr0()
995 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && in kvm_set_cr0()
1020 if (vcpu->arch.guest_state_protected) in kvm_load_guest_xsave_state()
1025 if (vcpu->arch.xcr0 != host_xcr0) in kvm_load_guest_xsave_state()
1026 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); in kvm_load_guest_xsave_state()
1029 vcpu->arch.ia32_xss != host_xss) in kvm_load_guest_xsave_state()
1030 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); in kvm_load_guest_xsave_state()
1034 vcpu->arch.pkru != vcpu->arch.host_pkru && in kvm_load_guest_xsave_state()
1035 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_guest_xsave_state()
1037 write_pkru(vcpu->arch.pkru); in kvm_load_guest_xsave_state()
1043 if (vcpu->arch.guest_state_protected) in kvm_load_host_xsave_state()
1047 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_host_xsave_state()
1049 vcpu->arch.pkru = rdpkru(); in kvm_load_host_xsave_state()
1050 if (vcpu->arch.pkru != vcpu->arch.host_pkru) in kvm_load_host_xsave_state()
1051 write_pkru(vcpu->arch.host_pkru); in kvm_load_host_xsave_state()
1056 if (vcpu->arch.xcr0 != host_xcr0) in kvm_load_host_xsave_state()
1060 vcpu->arch.ia32_xss != host_xss) in kvm_load_host_xsave_state()
1070 return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; in kvm_guest_supported_xfd()
1077 u64 old_xcr0 = vcpu->arch.xcr0; in __kvm_set_xcr()
1093 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; in __kvm_set_xcr()
1112 vcpu->arch.xcr0 = xcr0; in __kvm_set_xcr()
1137 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) in __kvm_is_valid_cr4()
1156 * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB in kvm_post_set_cr4()
1170 * - CR4.PCIDE is changed from 1 to 0 in kvm_post_set_cr4()
1171 * - CR4.PGE is toggled in kvm_post_set_cr4()
1182 * - CR4.SMEP is changed from 0 to 1 in kvm_post_set_cr4()
1183 * - CR4.PAE is toggled in kvm_post_set_cr4()
1225 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_invalidate_pcid()
1260 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) in kvm_invalidate_pcid()
1263 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); in kvm_invalidate_pcid()
1296 vcpu->arch.cr3 = cr3; in kvm_set_cr3()
1305 * and it's impossible to use a non-zero PCID when PCID is disabled, in kvm_set_cr3()
1322 vcpu->arch.cr8 = cr8; in kvm_set_cr8()
1332 return vcpu->arch.cr8; in kvm_get_cr8()
1340 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { in kvm_update_dr0123()
1342 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_update_dr0123()
1350 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) in kvm_update_dr7()
1351 dr7 = vcpu->arch.guest_debug_dr7; in kvm_update_dr7()
1353 dr7 = vcpu->arch.dr7; in kvm_update_dr7()
1355 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1357 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1375 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_set_dr()
1379 vcpu->arch.db[array_index_nospec(dr, size)] = val; in kvm_set_dr()
1380 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) in kvm_set_dr()
1381 vcpu->arch.eff_db[dr] = val; in kvm_set_dr()
1387 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); in kvm_set_dr()
1393 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; in kvm_set_dr()
1404 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_get_dr()
1408 *val = vcpu->arch.db[array_index_nospec(dr, size)]; in kvm_get_dr()
1412 *val = vcpu->arch.dr6; in kvm_get_dr()
1416 *val = vcpu->arch.dr7; in kvm_get_dr()
1573 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
1585 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
1610 * 10 - MISC_PACKAGE_CTRLS
1611 * 11 - ENERGY_FILTERING_CTL
1612 * 12 - DOITM
1613 * 18 - FB_CLEAR_CTRL
1614 * 21 - XAPIC_DISABLE_STATUS
1615 * 23 - OVERCLOCKING_STATUS
1683 switch (msr->index) { in kvm_get_msr_feature()
1685 msr->data = kvm_get_arch_capabilities(); in kvm_get_msr_feature()
1688 msr->data = kvm_caps.supported_perf_cap; in kvm_get_msr_feature()
1691 rdmsrl_safe(msr->index, &msr->data); in kvm_get_msr_feature()
1754 u64 old_efer = vcpu->arch.efer; in set_efer()
1755 u64 efer = msr_info->data; in set_efer()
1761 if (!msr_info->host_initiated) { in set_efer()
1766 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1771 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1795 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1804 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1806 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1812 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1813 ranges = msr_filter->ranges; in kvm_msr_allowed()
1815 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1822 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1828 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1837 * Returns 0 on success, non-0 otherwise.
1858 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1859 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1860 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1862 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1864 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1866 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1885 * the bits in all other cases. This ensures cross-vendor in __kvm_set_msr()
1917 * Returns 0 on success, non-0 otherwise.
1990 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1991 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1992 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
1998 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
2009 return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
2038 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2041 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2042 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2043 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2044 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2045 vcpu->run->msr.index = index; in kvm_msr_user_space()
2046 vcpu->run->msr.data = data; in kvm_msr_user_space()
2047 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2063 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2064 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2124 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) && in kvm_emulate_monitor_mwait()
2146 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || in kvm_vcpu_exit_request()
2152 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2159 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2166 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2237 return -EINVAL; in do_set_msr()
2272 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2275 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2276 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2277 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2278 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2279 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2280 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2281 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2283 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2284 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2285 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2286 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2287 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2288 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2289 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2291 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2293 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2295 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2300 /* Count up from boot time, but with the frequency of the raw clock. */ in get_kvmclock_base_ns()
2327 ++version; /* first time write, random junk */ in kvm_write_wall_clock()
2335 * The guest calculates current wall clock time by adding in kvm_write_wall_clock()
2336 * system time (updated by kvm_guest_time_update below) to the in kvm_write_wall_clock()
2339 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_write_wall_clock()
2342 wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ in kvm_write_wall_clock()
2360 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2362 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2363 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2366 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2369 vcpu->arch.time = system_time; in kvm_write_system_time()
2374 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2377 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2400 shift--; in kvm_get_time_scale()
2445 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2446 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2450 return -1; in set_tsc_khz()
2454 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2459 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2461 return -1; in set_tsc_khz()
2477 return -1; in kvm_set_tsc_khz()
2482 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2483 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2484 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2492 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2504 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2505 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2506 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2507 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2522 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2525 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2526 atomic_read(&vcpu->kvm->online_vcpus)); in kvm_track_tsc_matching()
2536 if (ka->use_master_clock || in kvm_track_tsc_matching()
2537 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) in kvm_track_tsc_matching()
2540 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, in kvm_track_tsc_matching()
2541 atomic_read(&vcpu->kvm->online_vcpus), in kvm_track_tsc_matching()
2542 ka->use_master_clock, gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2549 * The most significant 64-N bits (mult) of ratio represent the
2552 * point number (mult + frac * 2^(-N)).
2575 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); in kvm_compute_l1_tsc_offset()
2577 return target_tsc - tsc; in kvm_compute_l1_tsc_offset()
2582 return vcpu->arch.l1_tsc_offset + in kvm_read_l1_tsc()
2583 kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); in kvm_read_l1_tsc()
2614 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in kvm_vcpu_write_tsc_offset()
2615 vcpu->arch.l1_tsc_offset, in kvm_vcpu_write_tsc_offset()
2618 vcpu->arch.l1_tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2626 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( in kvm_vcpu_write_tsc_offset()
2631 vcpu->arch.tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2638 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2642 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( in kvm_vcpu_write_tsc_multiplier()
2646 vcpu->arch.tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2656 * TSC is marked unstable when we're running on Hyper-V, in kvm_check_tsc_unstable()
2671 u64 ns, bool matched) in __kvm_synchronize_tsc() argument
2673 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2675 lockdep_assert_held(&kvm->arch.tsc_write_lock); in __kvm_synchronize_tsc()
2678 * We also track th most recent recorded KHZ, write and time to in __kvm_synchronize_tsc()
2681 kvm->arch.last_tsc_nsec = ns; in __kvm_synchronize_tsc()
2682 kvm->arch.last_tsc_write = tsc; in __kvm_synchronize_tsc()
2683 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; in __kvm_synchronize_tsc()
2684 kvm->arch.last_tsc_offset = offset; in __kvm_synchronize_tsc()
2686 vcpu->arch.last_guest_tsc = tsc; in __kvm_synchronize_tsc()
2694 * nanosecond time, offset, and write, so if TSCs are in in __kvm_synchronize_tsc()
2698 * These values are tracked in kvm->arch.cur_xxx variables. in __kvm_synchronize_tsc()
2700 kvm->arch.cur_tsc_generation++; in __kvm_synchronize_tsc()
2701 kvm->arch.cur_tsc_nsec = ns; in __kvm_synchronize_tsc()
2702 kvm->arch.cur_tsc_write = tsc; in __kvm_synchronize_tsc()
2703 kvm->arch.cur_tsc_offset = offset; in __kvm_synchronize_tsc()
2704 kvm->arch.nr_vcpus_matched_tsc = 0; in __kvm_synchronize_tsc()
2705 } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { in __kvm_synchronize_tsc()
2706 kvm->arch.nr_vcpus_matched_tsc++; in __kvm_synchronize_tsc()
2710 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; in __kvm_synchronize_tsc()
2711 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; in __kvm_synchronize_tsc()
2712 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; in __kvm_synchronize_tsc()
2719 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2720 u64 offset, ns, elapsed; in kvm_synchronize_tsc() local
2725 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2727 ns = get_kvmclock_base_ns(); in kvm_synchronize_tsc()
2728 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2730 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2733 * detection of vcpu initialization -- need to sync in kvm_synchronize_tsc()
2739 u64 tsc_exp = kvm->arch.last_tsc_write + in kvm_synchronize_tsc()
2741 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; in kvm_synchronize_tsc()
2744 * of virtual cycle time against real time is in kvm_synchronize_tsc()
2754 * TSC, we add elapsed time in this computation. We could let the in kvm_synchronize_tsc()
2759 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2761 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2770 __kvm_synchronize_tsc(vcpu, offset, data, ns, matched); in kvm_synchronize_tsc()
2771 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2777 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2783 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2786 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2802 * predictable (it's just a function of time and the likely is in read_tsc()
2818 switch (clock->vclock_mode) { in vgettsc()
2824 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2825 clock->mask; in vgettsc()
2834 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2835 clock->mask; in vgettsc()
2844 return v * clock->mult; in vgettsc()
2852 u64 ns; in do_monotonic_raw() local
2855 seq = read_seqcount_begin(&gtod->seq); in do_monotonic_raw()
2856 ns = gtod->raw_clock.base_cycles; in do_monotonic_raw()
2857 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode); in do_monotonic_raw()
2858 ns >>= gtod->raw_clock.shift; in do_monotonic_raw()
2859 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_monotonic_raw()
2860 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_monotonic_raw()
2861 *t = ns; in do_monotonic_raw()
2871 u64 ns; in do_realtime() local
2874 seq = read_seqcount_begin(&gtod->seq); in do_realtime()
2875 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2876 ns = gtod->clock.base_cycles; in do_realtime()
2877 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_realtime()
2878 ns >>= gtod->clock.shift; in do_realtime()
2879 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_realtime()
2881 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2882 ts->tv_nsec = ns; in do_realtime()
2917 * "timespecX" represents host monotonic time. "tscX" represents
2926 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2927 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2928 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2930 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2932 * - ret0 < ret1
2933 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2935 * - 0 < N - M => M < N
2944 * copy of host monotonic time values. Update that master copy
2954 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
2958 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
2959 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
2960 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
2967 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
2968 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
2970 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
2971 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
2972 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
2974 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
2978 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
2990 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
2991 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3004 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3008 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3009 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3028 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3042 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3045 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3051 data->flags = 0; in __get_kvmclock()
3052 if (ka->use_master_clock && in __get_kvmclock()
3057 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3058 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3059 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3062 data->host_tsc = rdtsc(); in __get_kvmclock()
3064 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3065 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3066 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3070 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3072 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3080 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3084 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3086 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3101 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_setup_guest_pvclock()
3105 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3107 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3112 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3115 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3124 guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3128 vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3130 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_setup_guest_pvclock()
3131 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; in kvm_setup_guest_pvclock()
3132 vcpu->pvclock_set_guest_stopped_request = false; in kvm_setup_guest_pvclock()
3135 memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); in kvm_setup_guest_pvclock()
3138 guest_hv_clock->version = ++vcpu->hv_clock.version; in kvm_setup_guest_pvclock()
3140 mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); in kvm_setup_guest_pvclock()
3141 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3143 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); in kvm_setup_guest_pvclock()
3150 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3151 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3165 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3166 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3168 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3169 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3171 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3190 * time for two reasons, even if kvmclock is used. in kvm_guest_time_update()
3195 * time to disappear, and the guest to stand still or run in kvm_guest_time_update()
3198 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3201 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3212 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3214 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3216 &vcpu->hv_clock.tsc_shift, in kvm_guest_time_update()
3217 &vcpu->hv_clock.tsc_to_system_mul); in kvm_guest_time_update()
3218 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3222 vcpu->hv_clock.tsc_timestamp = tsc_timestamp; in kvm_guest_time_update()
3223 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3224 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3231 vcpu->hv_clock.flags = pvclock_flags; in kvm_guest_time_update()
3233 if (vcpu->pv_time.active) in kvm_guest_time_update()
3234 kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0); in kvm_guest_time_update()
3235 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3236 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3237 offsetof(struct compat_vcpu_info, time)); in kvm_guest_time_update()
3238 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3239 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0); in kvm_guest_time_update()
3240 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); in kvm_guest_time_update()
3246 * vcpu->cpu migration, should not allow system_timestamp from
3252 * We need to rate-limit these requests though, as they can
3254 * The time for a remote vcpu to update its kvmclock is bound
3255 * by the delay we use to rate-limit the updates.
3277 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3280 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3296 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3297 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3318 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3325 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3327 u32 msr = msr_info->index; in set_msr_mce()
3328 u64 data = msr_info->data; in set_msr_mce()
3333 vcpu->arch.mcg_status = data; in set_msr_mce()
3337 (data || !msr_info->host_initiated)) in set_msr_mce()
3341 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3343 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3344 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3348 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3353 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3354 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3355 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3357 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3358 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3370 * single-bit ECC data errors. in set_msr_mce()
3378 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3381 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3385 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3386 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3387 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3399 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3421 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3429 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3433 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3434 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3443 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3450 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3452 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3459 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3460 vcpu->arch.time = 0; in kvmclock_reset()
3465 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3474 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3490 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3499 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3507 * prior before nested VM-Enter/VM-Exit.
3521 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3524 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3528 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3533 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3536 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3539 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3541 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3542 gpa != ghc->gpa || in record_steal_time()
3543 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3545 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3547 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3548 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3552 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3559 int err = -EFAULT; in record_steal_time()
3570 "+m" (st->preempted)); in record_steal_time()
3576 vcpu->arch.st.preempted = 0; in record_steal_time()
3578 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3589 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3590 vcpu->arch.st.preempted = 0; in record_steal_time()
3593 unsafe_get_user(version, &st->version, out); in record_steal_time()
3595 version += 1; /* first time write, random junk */ in record_steal_time()
3598 unsafe_put_user(version, &st->version, out); in record_steal_time()
3602 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3603 steal += current->sched_info.run_delay - in record_steal_time()
3604 vcpu->arch.st.last_steal; in record_steal_time()
3605 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3606 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3609 unsafe_put_user(version, &st->version, out); in record_steal_time()
3614 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3631 u32 msr = msr_info->index; in kvm_set_msr_common()
3632 u64 data = msr_info->data; in kvm_set_msr_common()
3634 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) in kvm_set_msr_common()
3648 if (msr_info->host_initiated) in kvm_set_msr_common()
3649 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3652 if (!msr_info->host_initiated) in kvm_set_msr_common()
3654 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3657 if (!msr_info->host_initiated) in kvm_set_msr_common()
3667 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3670 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3674 if (!msr_info->host_initiated && !guest_has_pred_cmd_msr(vcpu)) in kvm_set_msr_common()
3685 if (!msr_info->host_initiated && in kvm_set_msr_common()
3705 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3721 vcpu->arch.pat = data; in kvm_set_msr_common()
3735 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3736 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3739 * as well, otherwise guest's percpu pvclock time could jump. in kvm_set_msr_common()
3743 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3747 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3749 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3759 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3763 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3766 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3771 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3773 vcpu->arch.smbase = data; in kvm_set_msr_common()
3776 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3779 if (msr_info->host_initiated) { in kvm_set_msr_common()
3782 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3784 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3788 if (!msr_info->host_initiated && in kvm_set_msr_common()
3798 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3802 if (!msr_info->host_initiated) in kvm_set_msr_common()
3804 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3810 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3811 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3817 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3818 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3824 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
3830 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
3850 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
3864 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
3885 if (data & (-1ULL << 1)) in kvm_set_msr_common()
3888 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
3893 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
3894 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
3911 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
3928 msr_info->host_initiated); in kvm_set_msr_common()
3930 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
3938 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
3943 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
3946 if (!msr_info->host_initiated || in kvm_set_msr_common()
3950 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
3957 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
3961 if (!msr_info->host_initiated && in kvm_set_msr_common()
3968 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
3971 if (!msr_info->host_initiated && in kvm_set_msr_common()
3978 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
3987 * as to-be-saved, even if an MSRs isn't fully supported. in kvm_set_msr_common()
3989 if (msr_info->host_initiated && !data && in kvm_set_msr_common()
4002 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4012 data = vcpu->arch.mcg_cap; in get_msr_mce()
4017 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4020 data = vcpu->arch.mcg_status; in get_msr_mce()
4022 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4023 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4029 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4030 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4031 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4033 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4034 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4038 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4039 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4040 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4051 switch (msr_info->index) { in kvm_get_msr_common()
4073 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4080 msr_info->data = 0; in kvm_get_msr_common()
4086 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4088 msr_info->data = 0; in kvm_get_msr_common()
4091 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4094 if (!msr_info->host_initiated && in kvm_get_msr_common()
4097 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4100 if (!msr_info->host_initiated && in kvm_get_msr_common()
4103 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4106 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4115 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4120 if (msr_info->host_initiated) { in kvm_get_msr_common()
4121 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4122 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4124 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4125 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4128 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4132 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4137 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4139 msr_info->data = 3; in kvm_get_msr_common()
4153 msr_info->data = 1 << 24; in kvm_get_msr_common()
4156 msr_info->data = kvm_get_apic_base(vcpu); in kvm_get_msr_common()
4159 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4161 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4164 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4167 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4170 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4172 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4175 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4179 msr_info->data = 1000ULL; in kvm_get_msr_common()
4181 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4184 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4190 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4196 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4202 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4208 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4214 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4220 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4226 msr_info->data = 0; in kvm_get_msr_common()
4232 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4238 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4244 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4251 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4252 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4253 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4254 msr_info->host_initiated); in kvm_get_msr_common()
4256 if (!msr_info->host_initiated && in kvm_get_msr_common()
4259 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4263 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4271 msr_info->data = 0x20000000; in kvm_get_msr_common()
4284 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4285 msr_info->host_initiated); in kvm_get_msr_common()
4297 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4302 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4307 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4310 if (!msr_info->host_initiated && in kvm_get_msr_common()
4311 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4313 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4316 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4319 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4323 if (!msr_info->host_initiated && in kvm_get_msr_common()
4327 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4330 if (!msr_info->host_initiated && in kvm_get_msr_common()
4334 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4338 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4343 * to-be-saved, even if an MSR isn't fully supported. in kvm_get_msr_common()
4345 if (msr_info->host_initiated && in kvm_get_msr_common()
4346 kvm_is_msr_to_save(msr_info->index)) { in kvm_get_msr_common()
4347 msr_info->data = 0; in kvm_get_msr_common()
4369 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4391 r = -EFAULT; in msr_io()
4395 r = -E2BIG; in msr_io()
4400 entries = memdup_user(user_msrs->entries, size); in msr_io()
4408 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4409 r = -EFAULT; in msr_io()
4429 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4433 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4437 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4597 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4598 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4604 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4642 void __user *uaddr = (void __user*)(unsigned long)attr->addr; in kvm_get_attr_addr()
4644 if ((u64)(unsigned long)uaddr != attr->addr) in kvm_get_attr_addr()
4645 return ERR_PTR_USR(-EFAULT); in kvm_get_attr_addr()
4653 if (attr->group) in kvm_x86_dev_get_attr()
4654 return -ENXIO; in kvm_x86_dev_get_attr()
4659 switch (attr->attr) { in kvm_x86_dev_get_attr()
4662 return -EFAULT; in kvm_x86_dev_get_attr()
4665 return -ENXIO; in kvm_x86_dev_get_attr()
4671 if (attr->group) in kvm_x86_dev_has_attr()
4672 return -ENXIO; in kvm_x86_dev_has_attr()
4674 switch (attr->attr) { in kvm_x86_dev_has_attr()
4678 return -ENXIO; in kvm_x86_dev_has_attr()
4694 r = -EFAULT; in kvm_arch_dev_ioctl()
4701 r = -E2BIG; in kvm_arch_dev_ioctl()
4704 r = -EFAULT; in kvm_arch_dev_ioctl()
4705 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4708 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4720 r = -EFAULT; in kvm_arch_dev_ioctl()
4724 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4729 r = -EFAULT; in kvm_arch_dev_ioctl()
4736 r = -EFAULT; in kvm_arch_dev_ioctl()
4747 r = -EFAULT; in kvm_arch_dev_ioctl()
4754 r = -E2BIG; in kvm_arch_dev_ioctl()
4757 r = -EFAULT; in kvm_arch_dev_ioctl()
4758 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4772 r = -EFAULT; in kvm_arch_dev_ioctl()
4780 r = -EFAULT; in kvm_arch_dev_ioctl()
4787 r = -EINVAL; in kvm_arch_dev_ioctl()
4801 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4809 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4810 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4811 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
4818 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
4821 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
4822 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
4823 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
4827 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
4828 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
4829 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
4835 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
4837 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
4845 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
4847 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
4849 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4851 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
4859 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
4863 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
4866 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
4870 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
4872 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
4873 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
4877 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
4878 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
4881 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
4885 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
4888 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
4890 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
4891 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
4892 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
4895 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
4896 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
4898 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
4899 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
4901 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
4908 if (vcpu->preempted) { in kvm_arch_vcpu_put()
4909 if (!vcpu->arch.guest_state_protected) in kvm_arch_vcpu_put()
4910 vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu); in kvm_arch_vcpu_put()
4916 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
4917 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
4921 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
4925 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
4972 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
4983 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
4984 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
4986 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
4987 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
4993 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
4994 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
4996 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
4997 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
4999 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5000 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5002 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5017 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5018 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5019 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5029 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5035 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5038 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5041 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5043 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5056 * - none of the bits for Machine Check Exceptions are set
5057 * - both the VAL (valid) and UC (uncorrectable) bits are set
5058 * MCI_STATUS_PCC - Processor Context Corrupted
5059 * MCI_STATUS_S - Signaled as a Machine Check Exception
5060 * MCI_STATUS_AR - Software recoverable Action Required
5064 return !mce->mcg_status && in is_ucna()
5065 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5066 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5067 (mce->status & MCI_STATUS_UC); in is_ucna()
5072 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5074 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5075 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5076 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5077 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5080 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5084 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5092 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5094 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5096 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5097 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5099 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5108 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5109 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5115 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5117 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5118 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5124 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5125 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5126 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5127 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5128 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5133 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5134 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5135 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5136 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5156 * the only time there can be two queued exceptions is if there's a in kvm_vcpu_ioctl_x86_get_vcpu_events()
5157 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5158 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5161 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5162 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5163 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5164 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5166 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5171 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5176 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5177 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5188 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5189 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5190 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5196 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5197 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5199 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5200 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5201 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5202 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5203 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5205 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5206 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5207 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5208 events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5210 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5211 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5212 events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5214 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5217 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5218 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5219 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5220 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5222 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5224 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5227 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5228 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5229 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5230 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5231 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5238 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5244 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5246 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5247 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5248 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5249 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5250 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5252 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5254 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5255 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5258 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5259 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5260 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5263 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5264 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5265 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5266 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5272 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5273 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5276 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5278 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5280 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5282 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5283 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5284 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5285 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5286 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5287 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5288 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5290 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5291 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5292 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5293 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5295 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5297 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5298 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5299 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5300 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5303 static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5305 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5307 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5309 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5311 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5313 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5316 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5318 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5319 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5320 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5322 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5326 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5327 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5328 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5332 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5333 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5335 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5339 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5340 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5341 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5342 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5359 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5361 dbgregs->dr6 = val; in kvm_vcpu_ioctl_x86_get_debugregs()
5362 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5368 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5369 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5371 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5372 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5373 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5374 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5376 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); in kvm_vcpu_ioctl_x86_set_debugregs()
5378 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5379 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5401 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5404 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5407 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5408 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5414 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5415 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5421 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5424 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5425 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5427 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5434 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5438 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5439 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5440 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5441 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5450 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5452 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5453 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5455 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5457 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5459 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5463 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5475 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5476 return -EINVAL; in kvm_set_guest_paused()
5477 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5487 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5492 r = -ENXIO; in kvm_arch_tsc_has_attr()
5507 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5509 r = -EFAULT; in kvm_arch_tsc_get_attr()
5510 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5515 r = -ENXIO; in kvm_arch_tsc_get_attr()
5525 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5531 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5533 u64 offset, tsc, ns; in kvm_arch_tsc_set_attr() local
5537 r = -EFAULT; in kvm_arch_tsc_set_attr()
5541 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5543 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5544 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5545 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5547 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5548 ns = get_kvmclock_base_ns(); in kvm_arch_tsc_set_attr()
5550 __kvm_synchronize_tsc(vcpu, offset, tsc, ns, matched); in kvm_arch_tsc_set_attr()
5551 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5557 r = -ENXIO; in kvm_arch_tsc_set_attr()
5571 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5574 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5598 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5599 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5601 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5603 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5604 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5608 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5609 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5610 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5613 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5614 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5615 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5617 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5620 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5625 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5630 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5633 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5634 if (vcpu->arch.pv_cpuid.enforce) in kvm_vcpu_ioctl_enable_cap()
5639 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5646 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5662 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5668 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5674 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5681 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5696 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5714 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5717 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5724 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5728 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5735 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5739 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5742 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5749 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5751 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5755 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5757 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5763 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5769 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5779 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5782 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5785 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5787 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5793 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5802 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5813 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5822 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5834 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5844 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5853 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5854 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
5858 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5864 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5871 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
5884 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
5887 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5893 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5903 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5909 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5929 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5945 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
5955 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5965 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5966 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
5969 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
5970 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5971 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
5974 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
5980 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
5981 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5983 r = -E2BIG; in kvm_arch_vcpu_ioctl()
5995 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5996 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
5999 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6003 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6018 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6019 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6020 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6030 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6035 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6041 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6050 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6054 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6076 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6094 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6095 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6110 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6112 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6115 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6117 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6123 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6127 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6129 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6133 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6137 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6140 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6148 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6152 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6154 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6155 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6157 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6160 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6161 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6163 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6166 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6169 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6178 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6180 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6182 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6183 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6184 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6191 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6193 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6194 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6196 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6197 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6203 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6204 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6205 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6206 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6207 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6208 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6217 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6219 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6220 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6221 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6224 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6225 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6226 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6228 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6230 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6237 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6239 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6243 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6244 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6245 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6256 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6257 * VM-Exit. in kvm_arch_sync_dirty_log()
6270 return -ENXIO; in kvm_vm_ioctl_irq_line()
6272 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6273 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6283 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6284 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6286 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6288 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6289 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6293 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6297 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6298 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6299 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6301 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6304 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6311 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6312 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6316 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6320 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6321 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6324 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6325 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6326 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6327 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6332 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6333 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) in kvm_vm_ioctl_enable_cap()
6336 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6337 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6339 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6344 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6347 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && in kvm_vm_ioctl_enable_cap()
6349 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6350 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6351 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6352 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6353 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6359 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6363 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6367 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6371 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6372 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6374 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6378 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6379 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6382 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6383 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6387 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6388 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6395 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6402 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6404 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6409 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6413 r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6416 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6420 r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6423 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6424 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6427 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6431 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6432 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6434 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6438 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6439 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6442 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6443 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6444 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6447 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6450 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6451 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6454 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6455 if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6457 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6458 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6461 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6464 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6465 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6469 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6471 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6472 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6473 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6474 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6477 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6480 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6494 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6498 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6501 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6502 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6503 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6506 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6509 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6523 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6534 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6535 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6546 if (!user_range->nmsrs) in kvm_add_msr_filter()
6549 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6550 return -EINVAL; in kvm_add_msr_filter()
6552 if (!user_range->flags) in kvm_add_msr_filter()
6553 return -EINVAL; in kvm_add_msr_filter()
6555 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6557 return -EINVAL; in kvm_add_msr_filter()
6559 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6563 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6564 .flags = user_range->flags, in kvm_add_msr_filter()
6565 .base = user_range->base, in kvm_add_msr_filter()
6566 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6570 msr_filter->count++; in kvm_add_msr_filter()
6583 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6584 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6586 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6587 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6589 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6591 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6595 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6597 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6598 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6605 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6606 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6607 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6608 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6609 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6638 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6639 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6650 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6658 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6659 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6660 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6661 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6681 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6683 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6689 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6693 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6716 return -EFAULT; in kvm_vm_ioctl_get_clock()
6723 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6728 return -EFAULT; in kvm_vm_ioctl_set_clock()
6735 return -EINVAL; in kvm_vm_ioctl_set_clock()
6755 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
6758 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
6759 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
6762 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
6769 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
6771 int r = -ENOTTY; in kvm_arch_vm_ioctl()
6773 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
6790 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6791 r = -EINVAL; in kvm_arch_vm_ioctl()
6792 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6794 r = -EFAULT; in kvm_arch_vm_ioctl()
6799 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6806 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6808 r = -EEXIST; in kvm_arch_vm_ioctl()
6812 r = -EINVAL; in kvm_arch_vm_ioctl()
6813 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6832 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
6834 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
6837 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6844 r = -EFAULT; in kvm_arch_vm_ioctl()
6849 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6850 r = -EEXIST; in kvm_arch_vm_ioctl()
6851 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
6853 r = -ENOMEM; in kvm_arch_vm_ioctl()
6854 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
6855 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
6858 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6870 r = -ENXIO; in kvm_arch_vm_ioctl()
6876 r = -EFAULT; in kvm_arch_vm_ioctl()
6894 r = -ENXIO; in kvm_arch_vm_ioctl()
6903 r = -EFAULT; in kvm_arch_vm_ioctl()
6906 r = -ENXIO; in kvm_arch_vm_ioctl()
6907 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6912 r = -EFAULT; in kvm_arch_vm_ioctl()
6919 r = -EFAULT; in kvm_arch_vm_ioctl()
6922 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6923 r = -ENXIO; in kvm_arch_vm_ioctl()
6924 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6928 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6932 r = -ENXIO; in kvm_arch_vm_ioctl()
6933 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6938 r = -EFAULT; in kvm_arch_vm_ioctl()
6945 r = -EFAULT; in kvm_arch_vm_ioctl()
6948 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6949 r = -ENXIO; in kvm_arch_vm_ioctl()
6950 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6954 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6959 r = -EFAULT; in kvm_arch_vm_ioctl()
6962 r = -ENXIO; in kvm_arch_vm_ioctl()
6963 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6970 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6971 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6972 r = -EBUSY; in kvm_arch_vm_ioctl()
6974 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
6975 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6980 r = -EFAULT; in kvm_arch_vm_ioctl()
6989 r = -EFAULT; in kvm_arch_vm_ioctl()
6994 r = -EFAULT; in kvm_arch_vm_ioctl()
7000 r = -EFAULT; in kvm_arch_vm_ioctl()
7009 r = -EFAULT; in kvm_arch_vm_ioctl()
7025 r = -EINVAL; in kvm_arch_vm_ioctl()
7035 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7041 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7045 r = -ENOTTY; in kvm_arch_vm_ioctl()
7055 r = -EFAULT; in kvm_arch_vm_ioctl()
7059 r = -ENOTTY; in kvm_arch_vm_ioctl()
7069 r = -EFAULT; in kvm_arch_vm_ioctl()
7073 r = -ENOTTY; in kvm_arch_vm_ioctl()
7083 r = -EFAULT; in kvm_arch_vm_ioctl()
7097 return -EFAULT; in kvm_arch_vm_ioctl()
7103 r = -ENOTTY; in kvm_arch_vm_ioctl()
7165 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7170 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7175 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7180 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7248 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7253 len -= n; in vcpu_mmio_write()
7268 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7275 len -= n; in vcpu_mmio_read()
7297 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7302 /* NPT walks are always user-walks */ in translate_nested_gpa()
7304 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7312 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7315 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7322 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7326 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7334 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7336 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7343 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7348 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7349 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7350 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7362 bytes -= toread; in kvm_read_guest_virt_helper()
7376 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7382 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7387 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7389 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7435 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7440 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7441 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7442 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7453 bytes -= towrite; in kvm_write_guest_virt_helper()
7481 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7539 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7549 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7550 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7551 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7552 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7557 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7560 return -1; in vcpu_mmio_gva_to_gpa()
7591 if (vcpu->mmio_read_completed) { in read_prepare()
7593 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7594 vcpu->mmio_read_completed = 0; in read_prepare()
7629 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7631 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7657 bool write = ops->write; in emulator_read_write_onepage()
7659 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7668 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7669 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7670 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7678 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7684 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7689 bytes -= handled; in emulator_read_write_onepage()
7692 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7693 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7694 frag->gpa = gpa; in emulator_read_write_onepage()
7695 frag->data = val; in emulator_read_write_onepage()
7696 frag->len = bytes; in emulator_read_write_onepage()
7710 if (ops->read_write_prepare && in emulator_read_write()
7711 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7714 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7717 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7720 now = -addr & ~PAGE_MASK; in emulator_read_write()
7727 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
7730 bytes -= now; in emulator_read_write()
7738 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
7741 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
7743 vcpu->mmio_needed = 1; in emulator_read_write()
7744 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
7746 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
7747 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
7748 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
7749 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
7751 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
7791 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
7805 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
7809 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
7857 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
7873 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
7882 vcpu->arch.pio.port = port; in emulator_pio_in_out()
7883 vcpu->arch.pio.in = in; in emulator_pio_in_out()
7884 vcpu->arch.pio.count = count; in emulator_pio_in_out()
7885 vcpu->arch.pio.size = size; in emulator_pio_in_out()
7888 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
7890 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
7892 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
7893 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
7894 vcpu->run->io.size = size; in emulator_pio_in_out()
7895 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
7896 vcpu->run->io.count = count; in emulator_pio_in_out()
7897 vcpu->run->io.port = port; in emulator_pio_in_out()
7913 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
7914 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
7915 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
7916 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
7917 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
7925 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
7973 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
7974 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
7977 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8012 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8025 value = vcpu->arch.cr2; in emulator_get_cr()
8054 vcpu->arch.cr2 = val; in emulator_set_cr()
8067 res = -1; in emulator_set_cr()
8128 desc->type = var.type; in emulator_get_segment()
8129 desc->s = var.s; in emulator_get_segment()
8130 desc->dpl = var.dpl; in emulator_get_segment()
8131 desc->p = var.present; in emulator_get_segment()
8132 desc->avl = var.avl; in emulator_get_segment()
8133 desc->l = var.l; in emulator_get_segment()
8134 desc->d = var.db; in emulator_get_segment()
8135 desc->g = var.g; in emulator_get_segment()
8153 if (desc->g) in emulator_set_segment()
8155 var.type = desc->type; in emulator_set_segment()
8156 var.dpl = desc->dpl; in emulator_set_segment()
8157 var.db = desc->d; in emulator_set_segment()
8158 var.s = desc->s; in emulator_set_segment()
8159 var.l = desc->l; in emulator_set_segment()
8160 var.g = desc->g; in emulator_set_segment()
8161 var.avl = desc->avl; in emulator_set_segment()
8162 var.present = desc->p; in emulator_set_segment()
8227 return -EINVAL; in emulator_check_pmc()
8238 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8246 &ctxt->exception); in emulator_intercept()
8316 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8318 if (!kvm->vm_bugged) in emulator_vm_bugged()
8389 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8391 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8392 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8393 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8394 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8395 ctxt->exception.error_code); in inject_emulated_exception()
8397 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8410 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8411 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8412 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8419 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8424 ctxt->gpa_available = false; in init_emulate_ctxt()
8425 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8426 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8428 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8429 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8430 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8434 ctxt->interruptibility = 0; in init_emulate_ctxt()
8435 ctxt->have_exception = false; in init_emulate_ctxt()
8436 ctxt->exception.vector = -1; in init_emulate_ctxt()
8437 ctxt->perm_ok = false; in init_emulate_ctxt()
8440 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8445 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8450 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8451 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8452 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8458 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8459 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8460 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8468 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8482 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8483 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8495 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8498 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8499 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8501 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8503 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8504 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8505 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8506 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8509 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8510 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8513 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8518 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8520 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8521 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8539 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8541 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8549 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8578 if (!vcpu->arch.mmu->root_role.direct) { in reexecute_instruction()
8596 * retry instruction -> write #PF -> emulation fail -> retry in reexecute_instruction()
8597 * instruction -> ... in reexecute_instruction()
8599 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8610 /* The instructions are well-emulated on direct mmu. */ in reexecute_instruction()
8611 if (vcpu->arch.mmu->root_role.direct) { in reexecute_instruction()
8614 write_lock(&vcpu->kvm->mmu_lock); in reexecute_instruction()
8615 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; in reexecute_instruction()
8616 write_unlock(&vcpu->kvm->mmu_lock); in reexecute_instruction()
8619 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8626 * and it failed try to unshadow page and re-enter the in reexecute_instruction()
8629 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8645 last_retry_eip = vcpu->arch.last_retry_eip; in retry_instruction()
8646 last_retry_addr = vcpu->arch.last_retry_addr; in retry_instruction()
8649 * If the emulation is caused by #PF and it is non-page_table in retry_instruction()
8650 * writing instruction, it means the VM-EXIT is caused by shadow in retry_instruction()
8654 * Note: if the guest uses a non-page-table modifying instruction in retry_instruction()
8661 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; in retry_instruction()
8673 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) in retry_instruction()
8676 vcpu->arch.last_retry_eip = ctxt->eip; in retry_instruction()
8677 vcpu->arch.last_retry_addr = cr2_or_gpa; in retry_instruction()
8679 if (!vcpu->arch.mmu->root_role.direct) in retry_instruction()
8682 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in retry_instruction()
8707 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8709 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8710 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8711 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8712 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8713 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8785 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8786 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8787 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
8790 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
8791 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
8794 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
8795 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
8796 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
8797 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
8803 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
8807 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
8808 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
8822 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
8824 switch (ctxt->b) { in is_vmware_backdoor_opcode()
8841 switch (ctxt->b) { in is_vmware_backdoor_opcode()
8854 * (and wrong) when emulating on an intercepted fault-like exception[*], as
8864 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
8872 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
8882 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
8888 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
8895 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
8913 if (ctxt->have_exception && in x86_emulate_instruction()
8916 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
8919 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
8920 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
8938 * injecting single-step #DBs. in x86_emulate_instruction()
8941 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
8942 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
8944 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
8951 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
8952 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
8953 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
8962 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
8963 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
8970 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
8973 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
8974 ctxt->gpa_available = true; in x86_emulate_instruction()
8975 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
8979 ctxt->exception.address = 0; in x86_emulate_instruction()
8994 if (ctxt->have_exception) { in x86_emulate_instruction()
8995 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
8996 vcpu->mmio_needed = false; in x86_emulate_instruction()
8999 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9000 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9001 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9002 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9005 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9008 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9009 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9011 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9014 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9015 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9026 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9027 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9030 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9032 * of which are fault-like. in x86_emulate_instruction()
9034 if (!ctxt->have_exception || in x86_emulate_instruction()
9035 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9037 if (ctxt->is_branch) in x86_emulate_instruction()
9039 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9040 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9043 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9052 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9055 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9075 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9081 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9083 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9103 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9104 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9108 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9109 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9119 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9121 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9122 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9127 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9150 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9151 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9182 khz = freq->new; in tsc_khz_changed()
9203 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9230 * clocks, provided we notify them before. Thus time never in __kvmclock_cpufreq_notifier()
9236 * Changing the TSC frequency at arbitrary points in time in __kvmclock_cpufreq_notifier()
9256 * Note that because a guest time update may take place in __kvmclock_cpufreq_notifier()
9261 * and the acquisition of the spinlock, we must also ping the in __kvmclock_cpufreq_notifier()
9271 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9274 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9280 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9285 * time go backwards. in __kvmclock_cpufreq_notifier()
9289 * to kick the cpu out of guest context. Next time in __kvmclock_cpufreq_notifier()
9303 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9305 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9308 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9336 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9337 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9369 * region to prevent possible deadlocks against time accessors which
9395 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9408 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9418 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9421 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9439 return -EIO; in kvm_x86_check_processor_compatibility()
9456 return -EEXIST; in __kvm_x86_vendor_init()
9466 return -EOPNOTSUPP; in __kvm_x86_vendor_init()
9471 return -EOPNOTSUPP; in __kvm_x86_vendor_init()
9484 return -EIO; in __kvm_x86_vendor_init()
9490 return -ENOMEM; in __kvm_x86_vendor_init()
9496 r = -ENOMEM; in __kvm_x86_vendor_init()
9515 kvm_init_pmu_capability(ops->pmu_ops); in __kvm_x86_vendor_init()
9520 r = ops->hardware_setup(); in __kvm_x86_vendor_init()
9539 if (pi_inject_timer == -1) in __kvm_x86_vendor_init()
9548 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in __kvm_x86_vendor_init()
9634 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
9639 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
9641 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
9644 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
9659 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
9685 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9691 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9692 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9695 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9704 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9706 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9715 * @apicid - apicid of vcpu to be kicked.
9735 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9741 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9761 unsigned long *inhibits = &kvm->arch.apicv_inhibit_reasons; in kvm_apicv_init()
9763 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9777 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9783 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9785 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9786 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9790 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9800 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
9808 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
9813 ++vcpu->stat.hypercalls; in complete_hypercall_exit()
9822 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
9846 ret = -KVM_EPERM; in kvm_emulate_hypercall()
9850 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
9860 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in kvm_emulate_hypercall()
9873 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in kvm_emulate_hypercall()
9885 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
9886 if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) in kvm_emulate_hypercall()
9891 ret = -KVM_EINVAL; in kvm_emulate_hypercall()
9895 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in kvm_emulate_hypercall()
9896 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in kvm_emulate_hypercall()
9897 vcpu->run->hypercall.args[0] = gpa; in kvm_emulate_hypercall()
9898 vcpu->run->hypercall.args[1] = npages; in kvm_emulate_hypercall()
9899 vcpu->run->hypercall.args[2] = attrs; in kvm_emulate_hypercall()
9900 vcpu->run->hypercall.flags = 0; in kvm_emulate_hypercall()
9902 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in kvm_emulate_hypercall()
9904 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in kvm_emulate_hypercall()
9905 vcpu->arch.complete_userspace_io = complete_hypercall_exit; in kvm_emulate_hypercall()
9909 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
9917 ++vcpu->stat.hypercalls; in kvm_emulate_hypercall()
9932 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
9933 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
9934 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
9935 ctxt->have_exception = true; in emulator_fix_hypercall()
9942 &ctxt->exception); in emulator_fix_hypercall()
9947 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
9948 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
9951 /* Called within kvm->srcu read side. */
9954 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
9956 kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu); in post_kvm_run_save()
9957 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
9958 kvm_run->apic_base = kvm_get_apic_base(vcpu); in post_kvm_run_save()
9960 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
9961 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
9965 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
9978 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
9981 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
9984 max_irr = -1; in update_cr8_intercept()
9986 if (max_irr != -1) in update_cr8_intercept()
9998 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10002 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10011 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10014 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10016 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10017 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10018 vcpu->arch.exception.error_code, in kvm_inject_exception()
10019 vcpu->arch.exception.injected); in kvm_inject_exception()
10029 * injected as part of a previous VM-Enter, but weren't successfully delivered
10030 * and need to be re-injected.
10035 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10036 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10041 * instruction boundaries for asynchronous events. However, because VM-Exits
10047 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10070 * Process nested events first, as nested VM-Exit supercedes event in kvm_check_and_inject_events()
10071 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10072 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10080 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10084 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10093 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10095 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10098 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10102 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10106 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10108 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10112 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10113 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10116 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10117 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10121 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10124 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10125 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10132 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10133 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10134 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10136 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10137 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10141 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10146 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10148 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10149 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10150 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10155 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10157 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10161 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10162 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10163 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10164 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10171 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10172 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10178 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10183 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10184 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10190 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10193 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10194 r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10198 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10199 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10207 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10208 r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10212 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10213 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10218 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10223 r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10229 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10240 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10241 kvm_x86_ops.nested_ops->has_events(vcpu)) in kvm_check_and_inject_events()
10246 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10248 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10256 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10257 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10261 if (r == -EBUSY) { in kvm_check_and_inject_events()
10276 * up at the same time. To handle this scenario, allow two NMIs to be in process_nmi()
10282 if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10289 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10292 limit--; in process_nmi()
10294 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10295 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10297 if (vcpu->arch.nmi_pending && in process_nmi()
10299 vcpu->arch.nmi_pending--; in process_nmi()
10301 if (vcpu->arch.nmi_pending) in process_nmi()
10308 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10325 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10331 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10338 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10341 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10347 * pending. At the same time, KVM_REQ_EVENT may not be set as APICv was in __kvm_vcpu_update_apicv()
10351 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10356 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10376 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10388 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10393 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10411 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10414 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10417 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10420 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10430 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10432 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10441 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10443 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10444 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10447 if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10448 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10452 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10461 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10466 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10467 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10473 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10491 smp_send_reschedule(vcpu->cpu); in __kvm_request_immediate_exit()
10496 * Called within kvm->srcu read side.
10513 r = -EIO; in vcpu_enter_guest()
10523 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10533 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10557 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10558 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10567 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10573 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10576 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10577 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10584 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10601 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10602 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10603 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10604 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10605 vcpu->run->eoi.vector = in vcpu_enter_guest()
10606 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10618 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10619 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10620 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10625 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10626 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10627 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10634 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10635 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10642 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10643 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10660 ++vcpu->stat.req_event; in vcpu_enter_guest()
10666 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10701 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10702 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10707 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10710 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10731 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10749 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10750 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10752 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10754 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10755 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10756 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10757 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10768 * per-VM state, and responsing vCPUs must wait for the update in vcpu_enter_guest()
10786 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
10787 ++vcpu->stat.exits; in vcpu_enter_guest()
10796 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
10797 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
10813 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
10814 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
10816 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10821 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
10824 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
10829 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10834 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
10841 ++vcpu->stat.exits; in vcpu_enter_guest()
10846 * Wait until after servicing IRQs to account guest time so that any in vcpu_enter_guest()
10867 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
10870 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
10880 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
10886 /* Called within kvm->srcu read side. */
10893 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
10896 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
10904 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
10925 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
10935 switch(vcpu->arch.mp_state) { in vcpu_block()
10938 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
10939 vcpu->arch.mp_state = in vcpu_block()
10943 vcpu->arch.apf.halted = false; in vcpu_block()
10956 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
10957 !vcpu->arch.apf.halted); in kvm_vcpu_running()
10960 /* Called within kvm->srcu read side. */
10965 vcpu->arch.l1tf_flush_l1d = true; in vcpu_run()
10974 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
10994 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
10995 ++vcpu->stat.request_irq_exits; in vcpu_run()
11018 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11043 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11047 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11050 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11051 len = min(8u, frag->len); in complete_emulated_mmio()
11052 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11053 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11055 if (frag->len <= 8) { in complete_emulated_mmio()
11058 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11061 frag->data += len; in complete_emulated_mmio()
11062 frag->gpa += len; in complete_emulated_mmio()
11063 frag->len -= len; in complete_emulated_mmio()
11066 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11067 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11069 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11070 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11072 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11076 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11077 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11078 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11079 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11080 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11081 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11082 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11089 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11090 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11097 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11098 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11104 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11105 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11110 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11114 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11115 if (kvm_run->immediate_exit) { in kvm_arch_vcpu_ioctl_run()
11116 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11138 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11140 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11141 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11142 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11147 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || in kvm_arch_vcpu_ioctl_run()
11148 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { in kvm_arch_vcpu_ioctl_run()
11149 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11153 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11159 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11161 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11162 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11169 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11171 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11172 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11173 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11174 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11175 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11176 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11177 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11178 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11180 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11182 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11183 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11184 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11189 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11190 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11193 if (kvm_run->immediate_exit) { in kvm_arch_vcpu_ioctl_run()
11194 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11206 if (kvm_run->kvm_valid_regs) in kvm_arch_vcpu_ioctl_run()
11218 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11226 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11227 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11229 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11230 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11231 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11232 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11233 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11234 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11235 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11236 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11238 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11239 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11240 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11241 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11242 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11243 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11244 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11245 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11248 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11249 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11262 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11263 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11265 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11266 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11267 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11268 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11269 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11270 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11271 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11272 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11274 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11275 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11276 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11277 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11278 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11279 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11280 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11281 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11284 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11285 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11287 vcpu->arch.exception.pending = false; in __set_regs()
11288 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11305 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11308 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11309 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11310 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11311 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11312 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11313 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11315 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11316 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11319 sregs->idt.limit = dt.size; in __get_sregs_common()
11320 sregs->idt.base = dt.address; in __get_sregs_common()
11322 sregs->gdt.limit = dt.size; in __get_sregs_common()
11323 sregs->gdt.base = dt.address; in __get_sregs_common()
11325 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11326 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11329 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11330 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11331 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11332 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11333 sregs->apic_base = kvm_get_apic_base(vcpu); in __get_sregs_common()
11340 if (vcpu->arch.guest_state_protected) in __get_sregs()
11343 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11344 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11345 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11354 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11359 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11360 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11387 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11388 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11389 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11390 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11392 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11404 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11408 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11431 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11432 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11433 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11436 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11437 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11438 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11440 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11452 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11460 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11461 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11462 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11466 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11467 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11474 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11477 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11480 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11482 if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11486 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11487 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11489 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11493 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11494 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11505 return -EINVAL; in __set_sregs_common()
11507 apic_base_msr.data = sregs->apic_base; in __set_sregs_common()
11510 return -EINVAL; in __set_sregs_common()
11512 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11515 dt.size = sregs->idt.limit; in __set_sregs_common()
11516 dt.address = sregs->idt.base; in __set_sregs_common()
11518 dt.size = sregs->gdt.limit; in __set_sregs_common()
11519 dt.address = sregs->gdt.base; in __set_sregs_common()
11522 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11523 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11524 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11526 static_call_cond(kvm_x86_post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11528 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11530 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11531 static_call(kvm_x86_set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11533 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11534 static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11535 vcpu->arch.cr0 = sregs->cr0; in __set_sregs_common()
11537 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11538 static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11541 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11546 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11549 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11550 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11551 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11552 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11553 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11554 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11556 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11557 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11563 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11565 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11584 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11597 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
11598 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
11599 !(sregs2->efer & EFER_LMA); in __set_sregs2()
11602 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
11603 return -EINVAL; in __set_sregs2()
11605 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
11606 return -EINVAL; in __set_sregs2()
11615 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
11619 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
11646 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11649 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11655 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11664 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
11665 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
11669 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
11670 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
11673 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
11685 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
11686 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
11687 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
11689 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
11691 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11692 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11695 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11699 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
11700 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
11710 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
11725 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
11731 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
11733 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
11734 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
11735 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
11736 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
11737 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
11747 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
11752 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
11753 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
11754 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
11755 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
11756 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
11757 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
11758 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
11759 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
11760 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
11770 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
11775 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
11777 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
11778 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
11779 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
11780 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
11781 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
11782 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
11783 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
11784 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
11794 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
11795 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
11797 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
11798 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
11800 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
11802 vcpu, &vcpu->run->s.regs.events); in store_regs()
11807 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
11808 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
11809 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
11812 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
11813 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
11816 return -EINVAL; in sync_regs()
11818 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
11821 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
11822 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
11825 return -EINVAL; in sync_regs()
11827 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
11835 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
11839 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
11840 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
11842 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
11843 return -EINVAL; in kvm_arch_vcpu_precreate()
11853 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
11854 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
11855 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
11857 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm, vcpu, KVM_HOST_USES_PFN); in kvm_arch_vcpu_create()
11859 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
11860 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
11862 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
11868 if (irqchip_in_kernel(vcpu->kvm)) { in kvm_arch_vcpu_create()
11879 * Ignore the current per-VM APICv state so that vCPU creation in kvm_arch_vcpu_create()
11881 * will ensure the vCPU gets the correct state before VM-Entry. in kvm_arch_vcpu_create()
11884 vcpu->arch.apic->apicv_active = true; in kvm_arch_vcpu_create()
11890 r = -ENOMEM; in kvm_arch_vcpu_create()
11895 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
11897 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
11899 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
11901 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
11903 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
11905 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
11912 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
11917 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); in kvm_arch_vcpu_create()
11918 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); in kvm_arch_vcpu_create()
11920 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_arch_vcpu_create()
11924 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
11927 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
11928 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
11931 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
11938 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
11939 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
11943 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
11950 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
11952 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
11954 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
11956 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
11957 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
11958 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
11968 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
11970 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
11977 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
11979 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
11981 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
11982 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
11994 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
11995 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
11996 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12001 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12002 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12004 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12006 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12007 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12008 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12020 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12030 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12041 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12043 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12044 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12045 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12046 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12047 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12051 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12053 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12054 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12057 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12060 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12061 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12062 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12068 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12070 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12071 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12089 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12091 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12092 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12100 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12111 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12118 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12140 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12200 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_hardware_enable()
12202 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_hardware_enable()
12204 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_hardware_enable()
12205 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_hardware_enable()
12216 * bring the TSCs fully up to date with real time, as we aren't yet far in kvm_arch_hardware_enable()
12217 * enough into CPU bringup that we know how much real time has actually in kvm_arch_hardware_enable()
12228 * loose cycle time. This isn't too big a deal, since the loss will be in kvm_arch_hardware_enable()
12235 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_hardware_enable()
12245 * catchup mode. This will catchup all VCPUs to real time, but cannot in kvm_arch_hardware_enable()
12249 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_hardware_enable()
12251 kvm->arch.backwards_tsc_observed = true; in kvm_arch_hardware_enable()
12253 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_hardware_enable()
12254 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_hardware_enable()
12264 kvm->arch.last_tsc_nsec = 0; in kvm_arch_hardware_enable()
12265 kvm->arch.last_tsc_write = 0; in kvm_arch_hardware_enable()
12280 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12285 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12295 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_sched_in()
12296 if (pmu->version && unlikely(pmu->event_count)) { in kvm_arch_sched_in()
12297 pmu->need_cleanup = true; in kvm_arch_sched_in()
12305 kfree(to_kvm_hv(kvm)->hv_pa_pg); in kvm_arch_free_vm()
12316 return -EINVAL; in kvm_arch_init_vm()
12328 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12329 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); in kvm_arch_init_vm()
12330 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12333 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12334 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12336 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12338 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12339 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12340 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12341 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12343 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12345 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12347 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12348 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12349 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12352 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12353 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12356 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12357 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12397 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12398 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12415 * -errno: on error
12420 * GPA->HVA translation will not change. However, the HVA is a user
12432 /* Called with kvm->slots_lock held. */ in __x86_set_memory_region()
12434 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12438 if (slot && slot->npages) in __x86_set_memory_region()
12439 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12450 if (!slot || !slot->npages) in __x86_set_memory_region()
12453 old_npages = slot->npages; in __x86_set_memory_region()
12454 hva = slot->userspace_addr; in __x86_set_memory_region()
12484 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12490 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12496 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12500 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12504 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12505 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12517 kvfree(slot->arch.rmap[i]); in memslot_rmap_free()
12518 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12529 kvfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12530 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12538 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12545 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12548 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12549 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12551 return -ENOMEM; in memslot_rmap_alloc()
12561 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12569 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12589 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12591 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12593 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12594 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12595 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12600 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
12617 kvfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
12618 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
12620 return -ENOMEM; in kvm_alloc_memslot_metadata()
12629 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
12634 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
12649 return -EINVAL; in kvm_arch_prepare_memory_region()
12652 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
12653 return -EINVAL; in kvm_arch_prepare_memory_region()
12659 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
12661 return -EIO; in kvm_arch_prepare_memory_region()
12674 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
12684 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
12685 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
12705 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
12714 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
12732 * which can be collapsed into a single large-page spte. Later in kvm_mmu_slot_apply_flags()
12733 * page faults will create the large-page sptes. in kvm_mmu_slot_apply_flags()
12738 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
12761 * time mmu_lock is held. Flushing after dropping mmu_lock is in kvm_mmu_slot_apply_flags()
12763 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
12770 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
12779 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
12780 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
12781 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
12782 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
12784 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
12787 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
12788 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
12791 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
12794 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
12808 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
12812 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
12832 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
12839 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
12846 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
12852 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
12869 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
12870 kvm_x86_ops.nested_ops->has_events(vcpu)) in kvm_vcpu_has_events()
12895 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
12910 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
12913 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_in_kernel()
12934 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
12955 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
12963 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
12964 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
12985 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
12992 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
12995 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13004 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13005 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13013 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13022 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13026 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13029 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13031 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13038 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13047 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13055 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13064 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13077 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13086 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13104 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13119 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13120 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13128 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13151 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13154 if (work->wakeup_all) in kvm_arch_async_page_present()
13155 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13157 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13158 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13160 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13162 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13163 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13167 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13168 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13174 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13188 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13195 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13201 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13207 atomic_inc(&kvm->arch.noncoherent_dma_count); in kvm_arch_register_noncoherent_dma()
13213 atomic_dec(&kvm->arch.noncoherent_dma_count); in kvm_arch_unregister_noncoherent_dma()
13219 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13235 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13236 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13237 ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13238 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13241 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13253 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13254 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13258 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13262 ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13265 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13267 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13279 if (new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13282 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13292 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13325 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13331 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13333 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13344 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13357 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13358 return -EIO; in kvm_handle_memory_failure()
13368 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13420 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13439 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13443 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13446 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13447 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13448 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13449 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13451 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13454 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13457 frag->data += len; in complete_sev_es_emulated_mmio()
13458 frag->gpa += len; in complete_sev_es_emulated_mmio()
13459 frag->len -= len; in complete_sev_es_emulated_mmio()
13462 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13463 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13471 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13472 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13473 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13474 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13475 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13476 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13478 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13490 return -EINVAL; in kvm_sev_es_mmio_write()
13496 bytes -= handled; in kvm_sev_es_mmio_write()
13501 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13502 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13503 frag->len = bytes; in kvm_sev_es_mmio_write()
13504 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13505 frag->data = data; in kvm_sev_es_mmio_write()
13507 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13508 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13510 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13511 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13512 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13513 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13514 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13516 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13529 return -EINVAL; in kvm_sev_es_mmio_read()
13535 bytes -= handled; in kvm_sev_es_mmio_read()
13540 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13541 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13542 frag->len = bytes; in kvm_sev_es_mmio_read()
13543 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13544 frag->data = data; in kvm_sev_es_mmio_read()
13546 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13547 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13549 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13550 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13551 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13552 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13554 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13562 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13563 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13571 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13572 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13574 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13575 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13585 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13586 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13594 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13598 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13607 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13608 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13609 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13611 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13613 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13623 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13624 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13629 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13633 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13641 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13642 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()