Lines Matching +full:dram +full:- +full:access +full:- +full:quirk
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
167 * advancement entirely. Any other value is used as-is and disables adaptive
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;
323 * Return true if we want to ignore/silent this failed msr access.
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()
807 * else the access will fault indefinitely (and to emulate hardware). 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_mmu_honors_guest_mtrrs(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()
1576 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
1588 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
1613 * 10 - MISC_PACKAGE_CTRLS
1614 * 11 - ENERGY_FILTERING_CTL
1615 * 12 - DOITM
1616 * 18 - FB_CLEAR_CTRL
1617 * 21 - XAPIC_DISABLE_STATUS
1618 * 23 - OVERCLOCKING_STATUS
1686 switch (msr->index) { in kvm_get_msr_feature()
1688 msr->data = kvm_get_arch_capabilities(); in kvm_get_msr_feature()
1691 msr->data = kvm_caps.supported_perf_cap; in kvm_get_msr_feature()
1694 rdmsrl_safe(msr->index, &msr->data); in kvm_get_msr_feature()
1752 u64 old_efer = vcpu->arch.efer; in set_efer()
1753 u64 efer = msr_info->data; in set_efer()
1759 if (!msr_info->host_initiated) { in set_efer()
1764 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1769 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1797 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1806 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1808 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1814 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1815 ranges = msr_filter->ranges; in kvm_msr_allowed()
1817 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1824 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1830 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1839 * Returns 0 on success, non-0 otherwise.
1860 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1861 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1862 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1864 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1866 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1868 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1887 * the bits in all other cases. This ensures cross-vendor in __kvm_set_msr()
1919 * Returns 0 on success, non-0 otherwise.
1992 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1993 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1994 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
2000 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
2011 return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
2040 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2043 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2044 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2045 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2046 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2047 vcpu->run->msr.index = index; in kvm_msr_user_space()
2048 vcpu->run->msr.data = data; in kvm_msr_user_space()
2049 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2065 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2066 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2126 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) && in kvm_emulate_monitor_mwait()
2148 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || in kvm_vcpu_exit_request()
2154 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2161 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2168 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2239 return -EINVAL; in do_set_msr()
2274 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2277 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2278 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2279 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2280 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2281 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2282 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2283 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2285 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2286 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2287 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2288 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2289 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2290 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2291 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2293 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2295 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2297 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2357 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2359 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2360 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2363 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2366 vcpu->arch.time = system_time; in kvm_write_system_time()
2371 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2374 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2397 shift--; in kvm_get_time_scale()
2442 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2443 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2447 return -1; in set_tsc_khz()
2451 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2456 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2458 return -1; in set_tsc_khz()
2474 return -1; in kvm_set_tsc_khz()
2479 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2480 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2481 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2489 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2501 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2502 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2503 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2504 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2518 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2526 bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2527 atomic_read(&vcpu->kvm->online_vcpus)) && in kvm_track_tsc_matching()
2528 gtod_is_based_on_tsc(gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2536 if ((ka->use_master_clock && new_generation) || in kvm_track_tsc_matching()
2537 (ka->use_master_clock != use_master_clock)) 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()
2673 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2675 lockdep_assert_held(&kvm->arch.tsc_write_lock); 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()
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()
2720 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2726 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2729 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2731 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2738 } else if (kvm->arch.user_set_tsc) { 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()
2743 * Here lies UAPI baggage: when a user-initiated TSC write has in kvm_synchronize_tsc()
2754 * come from the kernel's default vCPU creation. Make the 1-second in kvm_synchronize_tsc()
2763 kvm->arch.user_set_tsc = true; in kvm_synchronize_tsc()
2772 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2774 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2784 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2790 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2796 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2799 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2831 switch (clock->vclock_mode) { in vgettsc()
2837 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2838 clock->mask; in vgettsc()
2847 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2848 clock->mask; in vgettsc()
2857 return v * clock->mult; in vgettsc()
2868 seq = read_seqcount_begin(>od->seq); in do_monotonic_raw()
2869 ns = gtod->raw_clock.base_cycles; in do_monotonic_raw()
2870 ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); in do_monotonic_raw()
2871 ns >>= gtod->raw_clock.shift; in do_monotonic_raw()
2872 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_monotonic_raw()
2873 } while (unlikely(read_seqcount_retry(>od->seq, seq))); in do_monotonic_raw()
2887 seq = read_seqcount_begin(>od->seq); in do_realtime()
2888 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2889 ns = gtod->clock.base_cycles; in do_realtime()
2890 ns += vgettsc(>od->clock, tsc_timestamp, &mode); in do_realtime()
2891 ns >>= gtod->clock.shift; in do_realtime()
2892 } while (unlikely(read_seqcount_retry(>od->seq, seq))); in do_realtime()
2894 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2895 ts->tv_nsec = ns; in do_realtime()
2939 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2940 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2941 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2945 * - ret0 < ret1
2946 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2948 * - 0 < N - M => M < N
2967 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
2971 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
2972 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
2973 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
2980 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
2981 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
2983 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
2984 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
2985 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
2987 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
2991 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
3003 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
3004 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3017 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3021 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3022 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3041 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3055 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3058 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3064 data->flags = 0; in __get_kvmclock()
3065 if (ka->use_master_clock && in __get_kvmclock()
3070 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3071 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3072 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3075 data->host_tsc = rdtsc(); in __get_kvmclock()
3077 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3078 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3079 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3083 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3085 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3093 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3097 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3099 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3115 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_setup_guest_pvclock()
3119 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3121 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3126 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3129 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3138 guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3142 vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3144 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_setup_guest_pvclock()
3145 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; in kvm_setup_guest_pvclock()
3146 vcpu->pvclock_set_guest_stopped_request = false; in kvm_setup_guest_pvclock()
3149 memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); in kvm_setup_guest_pvclock()
3152 guest_hv_clock->flags &= ~PVCLOCK_TSC_STABLE_BIT; in kvm_setup_guest_pvclock()
3156 guest_hv_clock->version = ++vcpu->hv_clock.version; in kvm_setup_guest_pvclock()
3158 mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); in kvm_setup_guest_pvclock()
3159 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3161 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); in kvm_setup_guest_pvclock()
3168 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3169 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3182 ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE; in kvm_guest_time_update()
3193 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3194 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3196 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3197 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3199 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3226 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3229 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3240 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3242 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3244 &vcpu->hv_clock.tsc_shift, in kvm_guest_time_update()
3245 &vcpu->hv_clock.tsc_to_system_mul); in kvm_guest_time_update()
3246 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3250 vcpu->hv_clock.tsc_timestamp = tsc_timestamp; in kvm_guest_time_update()
3251 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3252 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3259 vcpu->hv_clock.flags = pvclock_flags; in kvm_guest_time_update()
3261 if (vcpu->pv_time.active) in kvm_guest_time_update()
3262 kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0, false); in kvm_guest_time_update()
3264 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3265 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3268 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3269 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0, in kvm_guest_time_update()
3272 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); in kvm_guest_time_update()
3299 struct kvm_arch *ka = &kvm->arch; in kvm_get_wall_clock_epoch()
3305 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_get_wall_clock_epoch()
3308 if (!ka->use_master_clock) in kvm_get_wall_clock_epoch()
3330 hv_clock.tsc_timestamp = ka->master_cycle_now; in kvm_get_wall_clock_epoch()
3331 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in kvm_get_wall_clock_epoch()
3333 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_get_wall_clock_epoch()
3339 * since 1970-01-01. in kvm_get_wall_clock_epoch()
3345 return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - in kvm_get_wall_clock_epoch()
3349 return ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_get_wall_clock_epoch()
3354 * vcpu->cpu migration, should not allow system_timestamp from
3360 * We need to rate-limit these requests though, as they can
3363 * by the delay we use to rate-limit the updates.
3385 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3388 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3401 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3402 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3423 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3430 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3432 u32 msr = msr_info->index; in set_msr_mce()
3433 u64 data = msr_info->data; in set_msr_mce()
3438 vcpu->arch.mcg_status = data; in set_msr_mce()
3442 (data || !msr_info->host_initiated)) in set_msr_mce()
3446 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3448 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3449 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3453 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3458 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3459 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3460 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3462 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3463 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3475 * single-bit ECC data errors. in set_msr_mce()
3483 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3486 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3490 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3491 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3492 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3504 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3526 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3534 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3538 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3539 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3548 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3555 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3557 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3564 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3565 vcpu->arch.time = 0; in kvmclock_reset()
3570 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3579 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3595 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3604 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3612 * prior before nested VM-Enter/VM-Exit.
3626 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3629 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3633 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3638 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3641 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3644 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3646 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3647 gpa != ghc->gpa || in record_steal_time()
3648 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3650 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3652 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3653 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3657 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3664 int err = -EFAULT; in record_steal_time()
3675 "+m" (st->preempted)); in record_steal_time()
3681 vcpu->arch.st.preempted = 0; in record_steal_time()
3683 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3694 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3695 vcpu->arch.st.preempted = 0; in record_steal_time()
3698 unsafe_get_user(version, &st->version, out); in record_steal_time()
3703 unsafe_put_user(version, &st->version, out); in record_steal_time()
3707 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3708 steal += current->sched_info.run_delay - in record_steal_time()
3709 vcpu->arch.st.last_steal; in record_steal_time()
3710 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3711 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3714 unsafe_put_user(version, &st->version, out); in record_steal_time()
3719 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3736 u32 msr = msr_info->index; in kvm_set_msr_common()
3737 u64 data = msr_info->data; in kvm_set_msr_common()
3739 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) in kvm_set_msr_common()
3754 if (msr_info->host_initiated) in kvm_set_msr_common()
3755 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3758 if (!msr_info->host_initiated) in kvm_set_msr_common()
3760 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3763 if (!msr_info->host_initiated) in kvm_set_msr_common()
3773 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3776 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3782 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3810 if (!msr_info->host_initiated && in kvm_set_msr_common()
3837 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3849 vcpu->arch.pat = data; in kvm_set_msr_common()
3863 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3864 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3871 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3875 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3877 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3887 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3891 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3894 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3899 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3901 vcpu->arch.smbase = data; in kvm_set_msr_common()
3904 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3907 if (msr_info->host_initiated) { in kvm_set_msr_common()
3910 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3912 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3916 if (!msr_info->host_initiated && in kvm_set_msr_common()
3926 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3930 if (!msr_info->host_initiated) in kvm_set_msr_common()
3932 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3938 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3939 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3945 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3946 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3952 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
3958 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
3978 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
3992 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
4013 if (data & (-1ULL << 1)) in kvm_set_msr_common()
4016 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
4021 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4022 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4039 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
4057 msr_info->host_initiated); in kvm_set_msr_common()
4060 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
4068 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
4073 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
4076 if (!msr_info->host_initiated || in kvm_set_msr_common()
4080 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
4087 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
4091 if (!msr_info->host_initiated && in kvm_set_msr_common()
4098 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
4101 if (!msr_info->host_initiated && in kvm_set_msr_common()
4108 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
4117 * as to-be-saved, even if an MSRs isn't fully supported. in kvm_set_msr_common()
4119 if (msr_info->host_initiated && !data && in kvm_set_msr_common()
4132 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4142 data = vcpu->arch.mcg_cap; in get_msr_mce()
4147 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4150 data = vcpu->arch.mcg_status; in get_msr_mce()
4152 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4153 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4159 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4160 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4161 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4163 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4164 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4168 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4169 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4170 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4181 switch (msr_info->index) { in kvm_get_msr_common()
4204 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4210 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ in kvm_get_msr_common()
4211 msr_info->data = 0; in kvm_get_msr_common()
4217 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4219 msr_info->data = 0; in kvm_get_msr_common()
4222 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4225 if (!msr_info->host_initiated && in kvm_get_msr_common()
4228 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4231 if (!msr_info->host_initiated && in kvm_get_msr_common()
4234 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4237 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4246 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4251 if (msr_info->host_initiated) { in kvm_get_msr_common()
4252 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4253 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4255 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4256 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4259 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4263 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4268 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4270 msr_info->data = 3; in kvm_get_msr_common()
4284 msr_info->data = 1 << 24; in kvm_get_msr_common()
4287 msr_info->data = kvm_get_apic_base(vcpu); in kvm_get_msr_common()
4290 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4292 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4295 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4298 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4301 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4303 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4306 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4310 msr_info->data = 1000ULL; in kvm_get_msr_common()
4312 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4315 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4321 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4327 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4333 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4339 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4345 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4351 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4357 msr_info->data = 0; in kvm_get_msr_common()
4363 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4369 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4375 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4382 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4383 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4384 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4385 msr_info->host_initiated); in kvm_get_msr_common()
4387 if (!msr_info->host_initiated && in kvm_get_msr_common()
4390 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4394 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4402 msr_info->data = 0x20000000; in kvm_get_msr_common()
4416 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4417 msr_info->host_initiated); in kvm_get_msr_common()
4430 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4435 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4440 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4443 if (!msr_info->host_initiated && in kvm_get_msr_common()
4444 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4446 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4449 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4452 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4456 if (!msr_info->host_initiated && in kvm_get_msr_common()
4460 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4463 if (!msr_info->host_initiated && in kvm_get_msr_common()
4467 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4471 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4476 * to-be-saved, even if an MSR isn't fully supported. in kvm_get_msr_common()
4478 if (msr_info->host_initiated && in kvm_get_msr_common()
4479 kvm_is_msr_to_save(msr_info->index)) { in kvm_get_msr_common()
4480 msr_info->data = 0; in kvm_get_msr_common()
4502 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4524 r = -EFAULT; in msr_io()
4528 r = -E2BIG; in msr_io()
4533 entries = memdup_user(user_msrs->entries, size); in msr_io()
4541 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4542 r = -EFAULT; in msr_io()
4563 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4567 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4571 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4743 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4744 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4751 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4795 void __user *uaddr = (void __user*)(unsigned long)attr->addr; in kvm_get_attr_addr()
4797 if ((u64)(unsigned long)uaddr != attr->addr) in kvm_get_attr_addr()
4798 return ERR_PTR_USR(-EFAULT); in kvm_get_attr_addr()
4806 if (attr->group) in kvm_x86_dev_get_attr()
4807 return -ENXIO; in kvm_x86_dev_get_attr()
4812 switch (attr->attr) { in kvm_x86_dev_get_attr()
4815 return -EFAULT; in kvm_x86_dev_get_attr()
4818 return -ENXIO; in kvm_x86_dev_get_attr()
4824 if (attr->group) in kvm_x86_dev_has_attr()
4825 return -ENXIO; in kvm_x86_dev_has_attr()
4827 switch (attr->attr) { in kvm_x86_dev_has_attr()
4831 return -ENXIO; in kvm_x86_dev_has_attr()
4847 r = -EFAULT; in kvm_arch_dev_ioctl()
4854 r = -E2BIG; in kvm_arch_dev_ioctl()
4857 r = -EFAULT; in kvm_arch_dev_ioctl()
4858 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4861 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4873 r = -EFAULT; in kvm_arch_dev_ioctl()
4877 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4882 r = -EFAULT; in kvm_arch_dev_ioctl()
4889 r = -EFAULT; in kvm_arch_dev_ioctl()
4900 r = -EFAULT; in kvm_arch_dev_ioctl()
4907 r = -E2BIG; in kvm_arch_dev_ioctl()
4910 r = -EFAULT; in kvm_arch_dev_ioctl()
4911 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4927 r = -EFAULT; in kvm_arch_dev_ioctl()
4935 r = -EFAULT; in kvm_arch_dev_ioctl()
4942 r = -EINVAL; in kvm_arch_dev_ioctl()
4956 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4964 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4965 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4966 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
4973 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
4976 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
4977 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
4978 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
4982 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
4983 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
4984 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
4990 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
4992 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
5000 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
5002 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
5004 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5006 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
5014 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
5018 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
5021 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
5025 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
5027 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
5028 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
5032 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
5033 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
5036 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
5040 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
5043 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
5045 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
5046 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
5047 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
5050 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
5051 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
5053 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
5054 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
5056 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
5063 if (vcpu->preempted) { in kvm_arch_vcpu_put()
5064 if (!vcpu->arch.guest_state_protected) in kvm_arch_vcpu_put()
5065 vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu); in kvm_arch_vcpu_put()
5071 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
5072 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
5076 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
5080 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
5127 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
5138 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
5139 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
5141 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
5142 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5148 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5149 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5151 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5152 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5154 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5155 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5157 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5172 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5173 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5174 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5184 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5190 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5193 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5196 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5198 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5211 * - none of the bits for Machine Check Exceptions are set
5212 * - both the VAL (valid) and UC (uncorrectable) bits are set
5213 * MCI_STATUS_PCC - Processor Context Corrupted
5214 * MCI_STATUS_S - Signaled as a Machine Check Exception
5215 * MCI_STATUS_AR - Software recoverable Action Required
5219 return !mce->mcg_status && in is_ucna()
5220 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5221 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5222 (mce->status & MCI_STATUS_UC); in is_ucna()
5227 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5229 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5230 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5231 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5232 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5235 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5239 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5247 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5249 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5251 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5252 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5254 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5263 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5264 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5270 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5272 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5273 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5279 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5280 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5281 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5282 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5283 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5288 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5289 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5290 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5291 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5312 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5313 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5316 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5317 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5318 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5319 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5321 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5326 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5331 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5332 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5343 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5344 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5345 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5351 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5352 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5354 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5355 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5356 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5357 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5358 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5360 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5361 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5362 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5363 events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5365 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5366 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5367 events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5369 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5372 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5373 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5374 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5375 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5377 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5379 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5382 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5383 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5384 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5385 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5386 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5393 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5399 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5401 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5402 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5403 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5404 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5405 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5407 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5409 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5410 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5413 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5414 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5415 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5418 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5419 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5420 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5421 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5427 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5428 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5431 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5433 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5435 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5437 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5438 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5439 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5440 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5441 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5442 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5443 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5445 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5446 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5447 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5448 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5450 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5452 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5453 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5454 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5455 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5456 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5459 static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5461 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5463 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5465 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5467 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5469 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5472 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5474 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5475 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5476 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5478 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5482 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5483 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5484 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5488 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5489 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5491 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5495 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5496 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5497 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5498 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5515 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5517 dbgregs->dr6 = val; in kvm_vcpu_ioctl_x86_get_debugregs()
5518 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5524 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5525 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5527 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5528 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5529 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5530 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5532 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); in kvm_vcpu_ioctl_x86_set_debugregs()
5534 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5535 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5557 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5560 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5563 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5564 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5570 kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5571 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5577 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5580 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5581 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5583 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5590 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5594 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5595 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5596 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5597 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5606 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5608 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5609 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5611 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5613 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5615 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5619 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5631 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5632 return -EINVAL; in kvm_set_guest_paused()
5633 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5643 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5648 r = -ENXIO; in kvm_arch_tsc_has_attr()
5663 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5665 r = -EFAULT; in kvm_arch_tsc_get_attr()
5666 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5671 r = -ENXIO; in kvm_arch_tsc_get_attr()
5681 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5687 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5693 r = -EFAULT; in kvm_arch_tsc_set_attr()
5697 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5699 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5700 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5701 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5703 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5706 kvm->arch.user_set_tsc = true; in kvm_arch_tsc_set_attr()
5708 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5714 r = -ENXIO; in kvm_arch_tsc_set_attr()
5728 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5731 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5751 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5752 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5754 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5757 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5758 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5762 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5763 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5764 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5772 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5773 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5774 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5776 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5779 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5785 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5790 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5794 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5795 if (vcpu->arch.pv_cpuid.enforce) in kvm_vcpu_ioctl_enable_cap()
5800 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5807 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5823 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5829 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5835 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5842 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5857 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5875 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5878 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5885 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5889 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5896 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5900 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5903 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5910 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5912 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5916 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5918 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5924 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5930 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5940 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5943 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5946 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5948 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5954 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5963 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5974 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5983 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5995 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6005 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6014 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6015 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
6019 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6025 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6032 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6045 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6048 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6054 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6064 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6070 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6090 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6106 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
6116 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6126 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6127 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
6130 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
6131 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6132 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6135 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
6141 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6142 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6144 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6156 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6157 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6160 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6164 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6179 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6180 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6181 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6193 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6198 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6204 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6213 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6217 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6239 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6257 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6258 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6273 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6275 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6278 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6280 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6286 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6290 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6292 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6296 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6300 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6303 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6311 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6315 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6317 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6318 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6320 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6323 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6324 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6326 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6329 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6332 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6341 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6343 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6345 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6346 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6347 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6354 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6356 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6357 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6359 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6360 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6366 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6367 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6368 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6369 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6370 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6371 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6380 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6382 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6383 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6384 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6387 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6388 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6389 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6391 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6393 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6400 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6402 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6406 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6407 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6408 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6419 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6420 * VM-Exit. in kvm_arch_sync_dirty_log()
6436 return -ENXIO; in kvm_vm_ioctl_irq_line()
6438 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6439 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6449 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6450 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6452 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6454 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6455 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6459 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6463 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6464 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6465 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6467 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6470 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6477 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6478 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6482 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6486 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6487 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6490 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6491 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6492 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6493 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6498 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6499 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) in kvm_vm_ioctl_enable_cap()
6502 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6503 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6505 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6510 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6513 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && in kvm_vm_ioctl_enable_cap()
6515 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6516 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6517 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6518 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6519 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6525 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6529 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6533 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6537 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6538 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6540 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6544 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6545 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6548 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6549 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6553 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6554 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6561 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6568 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6570 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6575 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6579 r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6582 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6586 r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6589 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6590 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6593 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6597 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6598 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6600 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6604 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6605 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6608 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6609 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6610 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6613 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6616 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6617 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6620 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6621 if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6623 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6624 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6627 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6630 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6631 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6635 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6637 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6638 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6639 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6640 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6643 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6646 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6660 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6664 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6667 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6668 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6669 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6672 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6675 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6689 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6700 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6701 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6712 if (!user_range->nmsrs) in kvm_add_msr_filter()
6715 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6716 return -EINVAL; in kvm_add_msr_filter()
6718 if (!user_range->flags) in kvm_add_msr_filter()
6719 return -EINVAL; in kvm_add_msr_filter()
6721 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6723 return -EINVAL; in kvm_add_msr_filter()
6725 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6729 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6730 .flags = user_range->flags, in kvm_add_msr_filter()
6731 .base = user_range->base, in kvm_add_msr_filter()
6732 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6736 msr_filter->count++; in kvm_add_msr_filter()
6749 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6750 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6752 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6753 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6755 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6757 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6761 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6763 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6764 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6771 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6772 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6773 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6774 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6775 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6804 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6805 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6816 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6824 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6825 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6826 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6827 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6847 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6849 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6855 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6859 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6882 return -EFAULT; in kvm_vm_ioctl_get_clock()
6889 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6894 return -EFAULT; in kvm_vm_ioctl_set_clock()
6901 return -EINVAL; in kvm_vm_ioctl_set_clock()
6921 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
6924 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
6925 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
6928 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
6935 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
6937 int r = -ENOTTY; in kvm_arch_vm_ioctl()
6939 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
6956 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6957 r = -EINVAL; in kvm_arch_vm_ioctl()
6958 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6960 r = -EFAULT; in kvm_arch_vm_ioctl()
6965 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6972 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6974 r = -EEXIST; in kvm_arch_vm_ioctl()
6978 r = -EINVAL; in kvm_arch_vm_ioctl()
6979 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6998 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
7000 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
7003 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7010 r = -EFAULT; in kvm_arch_vm_ioctl()
7015 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7016 r = -EEXIST; in kvm_arch_vm_ioctl()
7017 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7019 r = -ENOENT; in kvm_arch_vm_ioctl()
7022 r = -ENOMEM; in kvm_arch_vm_ioctl()
7023 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
7024 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7027 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7039 r = -ENXIO; in kvm_arch_vm_ioctl()
7045 r = -EFAULT; in kvm_arch_vm_ioctl()
7063 r = -ENXIO; in kvm_arch_vm_ioctl()
7072 r = -EFAULT; in kvm_arch_vm_ioctl()
7075 r = -ENXIO; in kvm_arch_vm_ioctl()
7076 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7081 r = -EFAULT; in kvm_arch_vm_ioctl()
7088 r = -EFAULT; in kvm_arch_vm_ioctl()
7091 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7092 r = -ENXIO; in kvm_arch_vm_ioctl()
7093 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7097 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7101 r = -ENXIO; in kvm_arch_vm_ioctl()
7102 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7107 r = -EFAULT; in kvm_arch_vm_ioctl()
7114 r = -EFAULT; in kvm_arch_vm_ioctl()
7117 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7118 r = -ENXIO; in kvm_arch_vm_ioctl()
7119 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7123 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7128 r = -EFAULT; in kvm_arch_vm_ioctl()
7131 r = -ENXIO; in kvm_arch_vm_ioctl()
7132 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7139 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7140 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7141 r = -EBUSY; in kvm_arch_vm_ioctl()
7143 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
7144 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7149 r = -EFAULT; in kvm_arch_vm_ioctl()
7158 r = -EFAULT; in kvm_arch_vm_ioctl()
7163 r = -EFAULT; in kvm_arch_vm_ioctl()
7169 r = -EFAULT; in kvm_arch_vm_ioctl()
7178 r = -EFAULT; in kvm_arch_vm_ioctl()
7194 r = -EINVAL; in kvm_arch_vm_ioctl()
7204 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7210 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7214 r = -ENOTTY; in kvm_arch_vm_ioctl()
7224 r = -EFAULT; in kvm_arch_vm_ioctl()
7228 r = -ENOTTY; in kvm_arch_vm_ioctl()
7238 r = -EFAULT; in kvm_arch_vm_ioctl()
7242 r = -ENOTTY; in kvm_arch_vm_ioctl()
7253 r = -EFAULT; in kvm_arch_vm_ioctl()
7268 return -EFAULT; in kvm_arch_vm_ioctl()
7274 r = -ENOTTY; in kvm_arch_vm_ioctl()
7336 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7341 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7346 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7351 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7419 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7424 len -= n; in vcpu_mmio_write()
7439 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7446 len -= n; in vcpu_mmio_read()
7465 gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 access, in translate_nested_gpa() argument
7468 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7473 /* NPT walks are always user-walks */ in translate_nested_gpa()
7474 access |= PFERR_USER_MASK; in translate_nested_gpa()
7475 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7483 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7485 u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_mmu_gva_to_gpa_read() local
7486 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7493 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7495 u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_mmu_gva_to_gpa_write() local
7496 access |= PFERR_WRITE_MASK; in kvm_mmu_gva_to_gpa_write()
7497 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7501 /* uses this to access any guest's mapped memory without checking CPL */
7505 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7507 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7511 struct kvm_vcpu *vcpu, u64 access, in kvm_read_guest_virt_helper() argument
7514 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7519 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7520 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7521 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7533 bytes -= toread; in kvm_read_guest_virt_helper()
7547 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7548 u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_fetch_guest_virt() local
7553 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7558 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7560 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7573 u64 access = (static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_read_guest_virt() local
7582 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, in kvm_read_guest_virt()
7592 u64 access = 0; in emulator_read_std() local
7595 access |= PFERR_IMPLICIT_ACCESS; in emulator_read_std()
7597 access |= PFERR_USER_MASK; in emulator_read_std()
7599 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); in emulator_read_std()
7603 struct kvm_vcpu *vcpu, u64 access, in kvm_write_guest_virt_helper() argument
7606 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7611 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7612 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7613 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7624 bytes -= towrite; in kvm_write_guest_virt_helper()
7637 u64 access = PFERR_WRITE_MASK; in emulator_write_std() local
7640 access |= PFERR_IMPLICIT_ACCESS; in emulator_write_std()
7642 access |= PFERR_USER_MASK; in emulator_write_std()
7645 access, exception); in emulator_write_std()
7652 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7696 /* For APIC access vmexit */ in vcpu_is_mmio_gpa()
7712 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7713 u64 access = ((static_call(kvm_x86_get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0) in vcpu_mmio_gva_to_gpa() local
7722 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7723 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7724 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7725 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7730 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7733 return -1; in vcpu_mmio_gva_to_gpa()
7764 if (vcpu->mmio_read_completed) { in read_prepare()
7766 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7767 vcpu->mmio_read_completed = 0; in read_prepare()
7802 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7804 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7830 bool write = ops->write; in emulator_read_write_onepage()
7832 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7841 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7842 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7843 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7851 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7857 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7862 bytes -= handled; in emulator_read_write_onepage()
7865 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7866 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7867 frag->gpa = gpa; in emulator_read_write_onepage()
7868 frag->data = val; in emulator_read_write_onepage()
7869 frag->len = bytes; in emulator_read_write_onepage()
7883 if (ops->read_write_prepare && in emulator_read_write()
7884 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7887 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7890 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7893 now = -addr & ~PAGE_MASK; in emulator_read_write()
7900 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
7903 bytes -= now; in emulator_read_write()
7911 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
7914 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
7916 vcpu->mmio_needed = 1; in emulator_read_write()
7917 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
7919 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
7920 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
7921 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
7922 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
7924 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
7964 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
7975 * enabled in the host and the access splits a cache line. in emulator_cmpxchg_emulated()
7978 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
7982 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
8015 * back the original value and the access is atomic, but KVM's ABI is in emulator_cmpxchg_emulated()
8040 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
8056 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
8065 vcpu->arch.pio.port = port; in emulator_pio_in_out()
8066 vcpu->arch.pio.in = in; in emulator_pio_in_out()
8067 vcpu->arch.pio.count = count; in emulator_pio_in_out()
8068 vcpu->arch.pio.size = size; in emulator_pio_in_out()
8071 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
8073 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
8075 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
8076 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
8077 vcpu->run->io.size = size; in emulator_pio_in_out()
8078 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
8079 vcpu->run->io.count = count; in emulator_pio_in_out()
8080 vcpu->run->io.port = port; in emulator_pio_in_out()
8096 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
8097 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
8098 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
8099 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
8100 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
8108 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
8156 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8157 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
8160 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8195 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8208 value = vcpu->arch.cr2; in emulator_get_cr()
8237 vcpu->arch.cr2 = val; in emulator_set_cr()
8250 res = -1; in emulator_set_cr()
8311 desc->type = var.type; in emulator_get_segment()
8312 desc->s = var.s; in emulator_get_segment()
8313 desc->dpl = var.dpl; in emulator_get_segment()
8314 desc->p = var.present; in emulator_get_segment()
8315 desc->avl = var.avl; in emulator_get_segment()
8316 desc->l = var.l; in emulator_get_segment()
8317 desc->d = var.db; in emulator_get_segment()
8318 desc->g = var.g; in emulator_get_segment()
8336 if (desc->g) in emulator_set_segment()
8338 var.type = desc->type; in emulator_set_segment()
8339 var.dpl = desc->dpl; in emulator_set_segment()
8340 var.db = desc->d; in emulator_set_segment()
8341 var.s = desc->s; in emulator_set_segment()
8342 var.l = desc->l; in emulator_set_segment()
8343 var.g = desc->g; in emulator_set_segment()
8344 var.avl = desc->avl; in emulator_set_segment()
8345 var.present = desc->p; in emulator_set_segment()
8410 return -EINVAL; in emulator_check_pmc()
8421 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8429 &ctxt->exception); in emulator_intercept()
8499 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8501 if (!kvm->vm_bugged) in emulator_vm_bugged()
8582 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8584 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8585 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8586 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8587 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8588 ctxt->exception.error_code); in inject_emulated_exception()
8590 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8603 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8604 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8605 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8612 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8617 ctxt->gpa_available = false; in init_emulate_ctxt()
8618 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8619 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8621 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8622 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8623 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8627 ctxt->interruptibility = 0; in init_emulate_ctxt()
8628 ctxt->have_exception = false; in init_emulate_ctxt()
8629 ctxt->exception.vector = -1; in init_emulate_ctxt()
8630 ctxt->perm_ok = false; in init_emulate_ctxt()
8633 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8638 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8643 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8644 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8645 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8651 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8652 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8653 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8661 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8675 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8676 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8688 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8691 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8692 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8694 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8696 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8697 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8698 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8699 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8702 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8703 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8706 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8711 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8713 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8714 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8732 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8734 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8742 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8771 if (!vcpu->arch.mmu->root_role.direct) { in reexecute_instruction()
8774 * write access need to be emulated. in reexecute_instruction()
8789 * retry instruction -> write #PF -> emulation fail -> retry in reexecute_instruction()
8790 * instruction -> ... in reexecute_instruction()
8792 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8803 /* The instructions are well-emulated on direct mmu. */ in reexecute_instruction()
8804 if (vcpu->arch.mmu->root_role.direct) { in reexecute_instruction()
8807 write_lock(&vcpu->kvm->mmu_lock); in reexecute_instruction()
8808 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; in reexecute_instruction()
8809 write_unlock(&vcpu->kvm->mmu_lock); in reexecute_instruction()
8812 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8818 * if emulation was due to access to shadowed page table in reexecute_instruction()
8819 * and it failed try to unshadow page and re-enter the in reexecute_instruction()
8822 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8825 * If the access faults on its page table, it can not in reexecute_instruction()
8838 last_retry_eip = vcpu->arch.last_retry_eip; in retry_instruction()
8839 last_retry_addr = vcpu->arch.last_retry_addr; in retry_instruction()
8842 * If the emulation is caused by #PF and it is non-page_table in retry_instruction()
8843 * writing instruction, it means the VM-EXIT is caused by shadow in retry_instruction()
8847 * Note: if the guest uses a non-page-table modifying instruction in retry_instruction()
8854 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; in retry_instruction()
8866 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) in retry_instruction()
8869 vcpu->arch.last_retry_eip = ctxt->eip; in retry_instruction()
8870 vcpu->arch.last_retry_addr = cr2_or_gpa; in retry_instruction()
8872 if (!vcpu->arch.mmu->root_role.direct) in retry_instruction()
8875 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in retry_instruction()
8900 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8902 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8903 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8904 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8905 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8906 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8978 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8979 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8980 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
8983 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
8984 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
8987 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
8988 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
8989 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
8990 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
8996 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
9000 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
9001 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
9015 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
9017 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9034 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9047 * (and wrong) when emulating on an intercepted fault-like exception[*], as
9057 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
9065 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
9075 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
9087 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
9094 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
9112 if (ctxt->have_exception && in x86_emulate_instruction()
9115 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
9118 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
9119 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
9137 * injecting single-step #DBs. in x86_emulate_instruction()
9140 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
9141 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
9143 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
9150 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9151 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
9152 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
9161 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
9162 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
9169 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
9172 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
9173 ctxt->gpa_available = true; in x86_emulate_instruction()
9174 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
9178 ctxt->exception.address = 0; in x86_emulate_instruction()
9193 if (ctxt->have_exception) { in x86_emulate_instruction()
9194 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9195 vcpu->mmio_needed = false; in x86_emulate_instruction()
9198 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9199 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9200 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9201 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9204 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9207 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9208 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9210 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9213 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9214 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9225 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9226 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9229 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9231 * of which are fault-like. in x86_emulate_instruction()
9233 if (!ctxt->have_exception || in x86_emulate_instruction()
9234 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9236 if (ctxt->is_branch) in x86_emulate_instruction()
9238 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9239 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9242 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9251 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9254 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9274 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9280 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9282 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9302 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9303 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9307 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9308 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9318 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9320 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9321 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9326 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9349 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9350 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9381 khz = freq->new; in tsc_khz_changed()
9402 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9470 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9473 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9479 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9502 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9504 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9507 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9535 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9536 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9594 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9607 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9617 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9620 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9638 return -EIO; in kvm_x86_check_processor_compatibility()
9655 return -EEXIST; in __kvm_x86_vendor_init()
9665 return -EOPNOTSUPP; in __kvm_x86_vendor_init()
9670 return -EOPNOTSUPP; in __kvm_x86_vendor_init()
9683 return -EIO; in __kvm_x86_vendor_init()
9689 return -ENOMEM; in __kvm_x86_vendor_init()
9695 r = -ENOMEM; in __kvm_x86_vendor_init()
9714 kvm_init_pmu_capability(ops->pmu_ops); in __kvm_x86_vendor_init()
9719 r = ops->hardware_setup(); in __kvm_x86_vendor_init()
9738 if (pi_inject_timer == -1) in __kvm_x86_vendor_init()
9747 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in __kvm_x86_vendor_init()
9833 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
9838 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
9840 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
9843 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
9858 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
9884 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9890 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9891 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9894 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9903 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9905 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9914 * @apicid - apicid of vcpu to be kicked.
9934 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9940 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9960 unsigned long *inhibits = &kvm->arch.apicv_inhibit_reasons; in kvm_apicv_init()
9962 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9976 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9982 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9984 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9985 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9989 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9999 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
10007 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
10012 ++vcpu->stat.hypercalls; in complete_hypercall_exit()
10021 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
10045 ret = -KVM_EPERM; in kvm_emulate_hypercall()
10049 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
10059 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in kvm_emulate_hypercall()
10072 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in kvm_emulate_hypercall()
10084 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
10085 if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) in kvm_emulate_hypercall()
10090 ret = -KVM_EINVAL; in kvm_emulate_hypercall()
10094 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in kvm_emulate_hypercall()
10095 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in kvm_emulate_hypercall()
10096 vcpu->run->hypercall.args[0] = gpa; in kvm_emulate_hypercall()
10097 vcpu->run->hypercall.args[1] = npages; in kvm_emulate_hypercall()
10098 vcpu->run->hypercall.args[2] = attrs; in kvm_emulate_hypercall()
10099 vcpu->run->hypercall.flags = 0; in kvm_emulate_hypercall()
10101 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in kvm_emulate_hypercall()
10103 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in kvm_emulate_hypercall()
10104 vcpu->arch.complete_userspace_io = complete_hypercall_exit; in kvm_emulate_hypercall()
10108 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
10116 ++vcpu->stat.hypercalls; in kvm_emulate_hypercall()
10128 * If the quirk is disabled, synthesize a #UD and let the guest pick up in emulator_fix_hypercall()
10131 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
10132 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
10133 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
10134 ctxt->have_exception = true; in emulator_fix_hypercall()
10141 &ctxt->exception); in emulator_fix_hypercall()
10146 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
10147 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
10150 /* Called within kvm->srcu read side. */
10153 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
10155 kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu); in post_kvm_run_save()
10156 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
10157 kvm_run->apic_base = kvm_get_apic_base(vcpu); in post_kvm_run_save()
10159 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
10160 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
10164 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
10177 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10180 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10183 max_irr = -1; in update_cr8_intercept()
10185 if (max_irr != -1) in update_cr8_intercept()
10197 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10201 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10210 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10213 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10215 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10216 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10217 vcpu->arch.exception.error_code, in kvm_inject_exception()
10218 vcpu->arch.exception.injected); in kvm_inject_exception()
10228 * injected as part of a previous VM-Enter, but weren't successfully delivered
10229 * and need to be re-injected.
10234 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10235 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10240 * instruction boundaries for asynchronous events. However, because VM-Exits
10246 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10269 * Process nested events first, as nested VM-Exit supersedes event in kvm_check_and_inject_events()
10270 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10271 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10279 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10283 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10292 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10294 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10297 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10301 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10305 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10307 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10311 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10312 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10315 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10316 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10320 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10323 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10324 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10331 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10332 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10333 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10335 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10336 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10340 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10345 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10347 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10348 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10349 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10354 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10356 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10360 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10361 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10362 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10363 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10370 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10371 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10377 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10382 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10383 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10389 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10392 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10393 r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10397 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10398 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10406 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10407 r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10411 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10412 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10417 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10422 r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10428 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10439 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10440 kvm_x86_ops.nested_ops->has_events(vcpu)) in kvm_check_and_inject_events()
10445 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10447 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10455 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10456 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10460 if (r == -EBUSY) { in kvm_check_and_inject_events()
10481 if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10488 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10491 limit--; in process_nmi()
10493 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10494 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10496 if (vcpu->arch.nmi_pending && in process_nmi()
10498 vcpu->arch.nmi_pending--; in process_nmi()
10500 if (vcpu->arch.nmi_pending) in process_nmi()
10507 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10524 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10530 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10537 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10540 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10550 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10555 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10571 * the vCPU would incorrectly be able to access the vAPIC page via MMIO in kvm_vcpu_update_apicv()
10573 * access page is sticky. in kvm_vcpu_update_apicv()
10575 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10587 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10592 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10610 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10613 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10616 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10619 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10629 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10631 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10640 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10642 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10643 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10646 if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10647 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10651 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10658 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10666 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10667 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10673 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10691 smp_send_reschedule(vcpu->cpu); in __kvm_request_immediate_exit()
10696 * Called within kvm->srcu read side.
10713 r = -EIO; in vcpu_enter_guest()
10723 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10733 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10757 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10758 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10769 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10775 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10778 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10779 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10786 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10803 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10804 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10805 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10806 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10807 vcpu->run->eoi.vector = in vcpu_enter_guest()
10808 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10821 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10822 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10823 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10828 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10829 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10830 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10837 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10838 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10845 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10846 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10864 ++vcpu->stat.req_event; in vcpu_enter_guest()
10870 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10905 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10906 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10911 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10914 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10935 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10953 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10954 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10956 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10958 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10959 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10960 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10961 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10972 * per-VM state, and responding vCPUs must wait for the update in vcpu_enter_guest()
10990 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
10991 ++vcpu->stat.exits; in vcpu_enter_guest()
10996 * since we do this before handling the vmexit, a DR access vmexit in vcpu_enter_guest()
11000 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
11001 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
11017 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
11018 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
11020 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11025 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
11028 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
11033 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11038 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
11045 ++vcpu->stat.exits; in vcpu_enter_guest()
11071 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
11074 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
11084 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
11090 /* Called within kvm->srcu read side. */
11097 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
11100 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
11108 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
11129 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
11139 switch(vcpu->arch.mp_state) { in vcpu_block()
11142 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
11143 vcpu->arch.mp_state = in vcpu_block()
11147 vcpu->arch.apf.halted = false; in vcpu_block()
11160 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
11161 !vcpu->arch.apf.halted); in kvm_vcpu_running()
11164 /* Called within kvm->srcu read side. */
11169 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; in vcpu_run()
11170 vcpu->arch.l1tf_flush_l1d = true; in vcpu_run()
11179 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11199 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11200 ++vcpu->stat.request_irq_exits; in vcpu_run()
11223 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11248 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11252 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11255 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11256 len = min(8u, frag->len); in complete_emulated_mmio()
11257 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11258 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11260 if (frag->len <= 8) { in complete_emulated_mmio()
11263 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11266 frag->data += len; in complete_emulated_mmio()
11267 frag->gpa += len; in complete_emulated_mmio()
11268 frag->len -= len; in complete_emulated_mmio()
11271 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11272 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11274 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11275 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11277 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11281 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11282 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11283 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11284 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11285 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11286 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11287 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11294 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11295 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11302 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11303 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11309 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11310 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11315 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11319 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11320 if (kvm_run->immediate_exit) { in kvm_arch_vcpu_ioctl_run()
11321 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11343 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11345 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11346 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11347 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11352 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || in kvm_arch_vcpu_ioctl_run()
11353 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { in kvm_arch_vcpu_ioctl_run()
11354 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11358 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11364 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11366 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11367 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11374 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11376 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11377 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11378 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11379 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11380 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11381 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11382 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11383 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11385 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11387 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11388 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11389 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11394 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11395 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11398 if (kvm_run->immediate_exit) { in kvm_arch_vcpu_ioctl_run()
11399 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11411 if (kvm_run->kvm_valid_regs) in kvm_arch_vcpu_ioctl_run()
11423 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11431 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11432 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11434 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11435 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11436 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11437 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11438 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11439 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11440 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11441 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11443 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11444 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11445 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11446 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11447 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11448 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11449 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11450 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11453 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11454 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11467 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11468 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11470 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11471 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11472 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11473 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11474 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11475 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11476 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11477 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11479 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11480 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11481 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11482 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11483 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11484 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11485 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11486 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11489 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11490 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11492 vcpu->arch.exception.pending = false; in __set_regs()
11493 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11510 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11513 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11514 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11515 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11516 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11517 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11518 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11520 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11521 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11524 sregs->idt.limit = dt.size; in __get_sregs_common()
11525 sregs->idt.base = dt.address; in __get_sregs_common()
11527 sregs->gdt.limit = dt.size; in __get_sregs_common()
11528 sregs->gdt.base = dt.address; in __get_sregs_common()
11530 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11531 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11534 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11535 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11536 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11537 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11538 sregs->apic_base = kvm_get_apic_base(vcpu); in __get_sregs_common()
11545 if (vcpu->arch.guest_state_protected) in __get_sregs()
11548 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11549 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11550 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11559 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11564 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11565 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11592 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11593 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11594 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11595 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11597 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11609 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11613 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11636 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11637 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11638 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11641 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11642 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11643 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11645 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11657 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11665 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11666 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11667 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11671 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11672 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11679 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11682 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11685 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11687 if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11691 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11692 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11694 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11698 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11699 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11710 return -EINVAL; in __set_sregs_common()
11712 apic_base_msr.data = sregs->apic_base; in __set_sregs_common()
11715 return -EINVAL; in __set_sregs_common()
11717 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11720 dt.size = sregs->idt.limit; in __set_sregs_common()
11721 dt.address = sregs->idt.base; in __set_sregs_common()
11723 dt.size = sregs->gdt.limit; in __set_sregs_common()
11724 dt.address = sregs->gdt.base; in __set_sregs_common()
11727 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11728 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11729 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11731 static_call_cond(kvm_x86_post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11733 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11735 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11736 static_call(kvm_x86_set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11738 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11739 static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11741 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11742 static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11745 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11750 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11753 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11754 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11755 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11756 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11757 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11758 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11760 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11761 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11767 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11769 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11790 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11803 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
11804 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
11805 !(sregs2->efer & EFER_LMA); in __set_sregs2()
11808 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
11809 return -EINVAL; in __set_sregs2()
11811 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
11812 return -EINVAL; in __set_sregs2()
11821 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
11825 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
11854 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11857 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11863 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11872 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
11873 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
11877 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
11878 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
11881 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
11893 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
11894 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
11895 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
11897 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
11899 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11900 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11903 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11907 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
11908 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
11918 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
11933 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
11939 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
11941 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
11942 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
11943 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
11944 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
11945 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
11955 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
11960 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
11961 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
11962 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
11963 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
11964 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
11965 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
11966 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
11967 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
11968 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
11978 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
11983 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
11985 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
11986 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
11987 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
11988 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
11989 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
11990 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
11991 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
11992 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
12002 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
12003 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
12005 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
12006 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
12008 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
12010 vcpu, &vcpu->run->s.regs.events); in store_regs()
12015 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
12016 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
12017 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
12020 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
12021 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
12024 return -EINVAL; in sync_regs()
12026 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
12029 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
12030 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
12033 return -EINVAL; in sync_regs()
12035 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
12043 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
12047 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12048 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
12050 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12051 return -EINVAL; in kvm_arch_vcpu_precreate()
12061 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
12062 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
12063 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
12065 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm, vcpu, KVM_HOST_USES_PFN); in kvm_arch_vcpu_create()
12067 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
12068 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
12070 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
12076 if (irqchip_in_kernel(vcpu->kvm)) { in kvm_arch_vcpu_create()
12087 * Ignore the current per-VM APICv state so that vCPU creation in kvm_arch_vcpu_create()
12089 * will ensure the vCPU gets the correct state before VM-Entry. in kvm_arch_vcpu_create()
12092 vcpu->arch.apic->apicv_active = true; in kvm_arch_vcpu_create()
12098 r = -ENOMEM; in kvm_arch_vcpu_create()
12103 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
12105 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
12107 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
12109 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
12111 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
12113 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
12120 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
12125 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); in kvm_arch_vcpu_create()
12126 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); in kvm_arch_vcpu_create()
12128 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_arch_vcpu_create()
12132 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
12135 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
12136 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
12139 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
12146 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
12147 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
12151 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
12158 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
12160 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
12162 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
12164 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
12165 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
12166 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
12176 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
12178 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12185 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12187 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12189 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12190 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12202 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12203 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12204 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12209 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12210 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12212 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12214 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12215 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12216 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12228 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12238 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12249 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12251 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12252 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12253 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12254 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12255 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12259 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12261 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12262 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12265 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12268 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12269 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12270 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12276 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12278 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12279 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12296 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12298 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12299 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12307 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12318 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12325 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12347 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12407 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_hardware_enable()
12409 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_hardware_enable()
12411 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_hardware_enable()
12412 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_hardware_enable()
12442 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_hardware_enable()
12456 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_hardware_enable()
12458 kvm->arch.backwards_tsc_observed = true; in kvm_arch_hardware_enable()
12460 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_hardware_enable()
12461 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_hardware_enable()
12471 kvm->arch.last_tsc_nsec = 0; in kvm_arch_hardware_enable()
12472 kvm->arch.last_tsc_write = 0; in kvm_arch_hardware_enable()
12487 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12492 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12502 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_sched_in()
12503 if (pmu->version && unlikely(pmu->event_count)) { in kvm_arch_sched_in()
12504 pmu->need_cleanup = true; in kvm_arch_sched_in()
12513 kfree(kvm->arch.hv_pa_pg); in kvm_arch_free_vm()
12525 return -EINVAL; in kvm_arch_init_vm()
12527 kvm->arch.vm_type = type; in kvm_arch_init_vm()
12539 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12540 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12543 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12544 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12546 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12548 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12549 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12550 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12551 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12553 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12555 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12557 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12558 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12559 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12562 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12563 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12566 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12567 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12607 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12608 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12625 * -errno: on error
12630 * GPA->HVA translation will not change. However, the HVA is a user
12642 /* Called with kvm->slots_lock held. */ in __x86_set_memory_region()
12644 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12648 if (slot && slot->npages) in __x86_set_memory_region()
12649 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12660 if (!slot || !slot->npages) in __x86_set_memory_region()
12663 old_npages = slot->npages; in __x86_set_memory_region()
12664 hva = slot->userspace_addr; in __x86_set_memory_region()
12694 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12700 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12706 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12710 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12714 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12715 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12727 kvfree(slot->arch.rmap[i]); in memslot_rmap_free()
12728 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12739 kvfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12740 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12748 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12755 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12758 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12759 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12761 return -ENOMEM; in memslot_rmap_alloc()
12771 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12779 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12799 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12801 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12803 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12804 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12805 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12810 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
12831 kvfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
12832 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
12834 return -ENOMEM; in kvm_alloc_memslot_metadata()
12843 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
12848 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
12863 return -EINVAL; in kvm_arch_prepare_memory_region()
12866 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
12867 return -EINVAL; in kvm_arch_prepare_memory_region()
12873 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
12875 return -EIO; in kvm_arch_prepare_memory_region()
12888 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
12898 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
12899 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
12919 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
12928 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
12946 * which can be collapsed into a single large-page spte. Later in kvm_mmu_slot_apply_flags()
12947 * page faults will create the large-page sptes. in kvm_mmu_slot_apply_flags()
12952 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
12972 * the subtly complex checks when removing write access. in kvm_mmu_slot_apply_flags()
12977 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
12984 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
12993 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
12994 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
12995 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
12996 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
12998 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
13001 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
13002 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
13005 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
13008 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
13022 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
13026 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
13046 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
13053 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
13060 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
13066 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
13083 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
13084 kvm_x86_ops.nested_ops->has_events(vcpu)) in kvm_vcpu_has_events()
13109 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
13124 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
13128 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_in_kernel()
13151 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
13172 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
13180 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
13181 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13202 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13209 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13212 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13221 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13222 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13230 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13239 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13243 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13246 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13248 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13255 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13264 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13272 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13281 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13294 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13303 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13321 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13336 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13337 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13345 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13368 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13371 if (work->wakeup_all) in kvm_arch_async_page_present()
13372 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13374 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13375 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13377 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13379 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13380 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13384 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13385 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13391 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13405 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13412 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13418 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13425 * Non-coherent DMA assignment and de-assignment will affect in kvm_noncoherent_dma_assignment_start_or_stop()
13428 * So, pass %true unconditionally to indicate non-coherent DMA was, in kvm_noncoherent_dma_assignment_start_or_stop()
13437 if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) in kvm_arch_register_noncoherent_dma()
13444 if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) in kvm_arch_unregister_noncoherent_dma()
13451 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13467 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13468 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13469 ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13470 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13473 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13485 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13486 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13490 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13494 ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13497 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13499 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13511 if (new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13514 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13524 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13557 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13559 u64 access = error_code & in kvm_fixup_and_inject_pf_error() local
13563 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13565 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13576 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13589 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13590 return -EIO; in kvm_handle_memory_failure()
13600 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13656 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13675 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13679 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13682 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13683 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13684 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13685 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13687 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13690 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13693 frag->data += len; in complete_sev_es_emulated_mmio()
13694 frag->gpa += len; in complete_sev_es_emulated_mmio()
13695 frag->len -= len; in complete_sev_es_emulated_mmio()
13698 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13699 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13707 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13708 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13709 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13710 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13711 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13712 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13714 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13726 return -EINVAL; in kvm_sev_es_mmio_write()
13732 bytes -= handled; in kvm_sev_es_mmio_write()
13737 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13738 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13739 frag->len = bytes; in kvm_sev_es_mmio_write()
13740 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13741 frag->data = data; in kvm_sev_es_mmio_write()
13743 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13744 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13746 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13747 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13748 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13749 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13750 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13752 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13765 return -EINVAL; in kvm_sev_es_mmio_read()
13771 bytes -= handled; in kvm_sev_es_mmio_read()
13776 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13777 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13778 frag->len = bytes; in kvm_sev_es_mmio_read()
13779 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13780 frag->data = data; in kvm_sev_es_mmio_read()
13782 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13783 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13785 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13786 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13787 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13788 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13790 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13798 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13799 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13807 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13808 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13810 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13811 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13821 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13822 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13830 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13834 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13843 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13844 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13845 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13847 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13849 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13859 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13860 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13865 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13869 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13877 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13878 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()