Lines Matching +full:ext +full:- +full:clk +full:- +full:freq +full:- +full:hz

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;
350 size - useroffset, NULL); in kvm_alloc_emulator_cache()
359 vcpu->arch.apf.gfns[i] = ~0; in kvm_async_pf_hash_reset()
375 if (msrs->registered) { in kvm_on_user_return()
376 msrs->registered = false; in kvm_on_user_return()
381 values = &msrs->values[slot]; in kvm_on_user_return()
382 if (values->host != values->curr) { in kvm_on_user_return()
383 wrmsrl(kvm_uret_msrs_list[slot], values->host); in kvm_on_user_return()
384 values->curr = values->host; in kvm_on_user_return()
409 return -1; in kvm_add_user_return_msr()
424 return -1; in kvm_find_user_return_msr()
437 msrs->values[i].host = value; in kvm_user_return_msr_cpu_online()
438 msrs->values[i].curr = value; in kvm_user_return_msr_cpu_online()
448 value = (value & mask) | (msrs->values[slot].host & ~mask); in kvm_set_user_return_msr()
449 if (value == msrs->values[slot].curr) in kvm_set_user_return_msr()
455 msrs->values[slot].curr = value; in kvm_set_user_return_msr()
456 if (!msrs->registered) { in kvm_set_user_return_msr()
457 msrs->urn.on_user_return = kvm_on_user_return; in kvm_set_user_return_msr()
458 user_return_notifier_register(&msrs->urn); in kvm_set_user_return_msr()
459 msrs->registered = true; in kvm_set_user_return_msr()
470 if (msrs->registered) in drop_user_return_notifiers()
471 kvm_on_user_return(&msrs->urn); in drop_user_return_notifiers()
476 return vcpu->arch.apic_base; in kvm_get_apic_base()
488 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); in kvm_set_apic_base()
492 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) in kvm_set_apic_base()
494 if (!msr_info->host_initiated) { in kvm_set_apic_base()
501 kvm_lapic_set_base(vcpu, msr_info->data); in kvm_set_apic_base()
502 kvm_recalculate_apic_map(vcpu->kvm); in kvm_set_apic_base()
557 * #DBs can be trap-like or fault-like, the caller must check other CPU in exception_type()
576 if (!ex->has_payload) in kvm_deliver_exception_payload()
579 switch (ex->vector) { in kvm_deliver_exception_payload()
582 * "Certain debug exceptions may clear bit 0-3. The in kvm_deliver_exception_payload()
586 vcpu->arch.dr6 &= ~DR_TRAP_BITS; in kvm_deliver_exception_payload()
595 * Active low bits should be cleared if 1-setting in payload. in kvm_deliver_exception_payload()
596 * Active high bits should be set if 1-setting in payload. in kvm_deliver_exception_payload()
603 vcpu->arch.dr6 |= DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
604 vcpu->arch.dr6 |= ex->payload; in kvm_deliver_exception_payload()
605 vcpu->arch.dr6 ^= ex->payload & DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
613 vcpu->arch.dr6 &= ~BIT(12); in kvm_deliver_exception_payload()
616 vcpu->arch.cr2 = ex->payload; in kvm_deliver_exception_payload()
620 ex->has_payload = false; in kvm_deliver_exception_payload()
621 ex->payload = 0; in kvm_deliver_exception_payload()
629 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; in kvm_queue_exception_vmexit()
631 ex->vector = vector; in kvm_queue_exception_vmexit()
632 ex->injected = false; in kvm_queue_exception_vmexit()
633 ex->pending = true; in kvm_queue_exception_vmexit()
634 ex->has_error_code = has_error_code; in kvm_queue_exception_vmexit()
635 ex->error_code = error_code; in kvm_queue_exception_vmexit()
636 ex->has_payload = has_payload; in kvm_queue_exception_vmexit()
637 ex->payload = payload; in kvm_queue_exception_vmexit()
643 kvm_x86_ops.nested_ops->leave_nested(vcpu); in kvm_leave_nested()
657 * morph it to a VM-Exit if L1 wants to intercept the exception. A in kvm_multiple_exception()
659 * when it was original queued, and re-checking is incorrect if _L1_ in kvm_multiple_exception()
663 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, nr, error_code)) { in kvm_multiple_exception()
669 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { in kvm_multiple_exception()
673 * On VM-Entry, an exception can be pending if and only in kvm_multiple_exception()
680 vcpu->arch.exception.injected = true; in kvm_multiple_exception()
690 vcpu->arch.exception.pending = true; in kvm_multiple_exception()
691 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
693 vcpu->arch.exception.has_error_code = has_error; in kvm_multiple_exception()
694 vcpu->arch.exception.vector = nr; in kvm_multiple_exception()
695 vcpu->arch.exception.error_code = error_code; in kvm_multiple_exception()
696 vcpu->arch.exception.has_payload = has_payload; in kvm_multiple_exception()
697 vcpu->arch.exception.payload = payload; in kvm_multiple_exception()
700 &vcpu->arch.exception); in kvm_multiple_exception()
705 prev_nr = vcpu->arch.exception.vector; in kvm_multiple_exception()
707 /* triple fault -> shutdown */ in kvm_multiple_exception()
719 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
720 vcpu->arch.exception.pending = false; in kvm_multiple_exception()
725 that instruction re-execution will regenerate lost in kvm_multiple_exception()
781 ++vcpu->stat.pf_guest; in kvm_inject_page_fault()
784 * Async #PF in L2 is always forwarded to L1 as a VM-Exit regardless of in kvm_inject_page_fault()
787 if (is_guest_mode(vcpu) && fault->async_page_fault) in kvm_inject_page_fault()
789 true, fault->error_code, in kvm_inject_page_fault()
790 true, fault->address); in kvm_inject_page_fault()
792 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, in kvm_inject_page_fault()
793 fault->address); in kvm_inject_page_fault()
800 WARN_ON_ONCE(fault->vector != PF_VECTOR); in kvm_inject_emulated_page_fault()
802 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : in kvm_inject_emulated_page_fault()
803 vcpu->arch.walk_mmu; in kvm_inject_emulated_page_fault()
809 if ((fault->error_code & PFERR_PRESENT_MASK) && in kvm_inject_emulated_page_fault()
810 !(fault->error_code & PFERR_RSVD_MASK)) in kvm_inject_emulated_page_fault()
811 kvm_mmu_invalidate_addr(vcpu, fault_mmu, fault->address, in kvm_inject_emulated_page_fault()
814 fault_mmu->inject_page_fault(vcpu, fault); in kvm_inject_emulated_page_fault()
820 atomic_inc(&vcpu->arch.nmi_queued); in kvm_inject_nmi()
860 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); in pdptr_rsvd_bits()
868 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in load_pdptrs()
873 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; in load_pdptrs()
901 if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) in load_pdptrs()
902 kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); in load_pdptrs()
904 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); in load_pdptrs()
907 vcpu->arch.pdptrs_from_userspace = false; in load_pdptrs()
932 * CR0.WP is incorporated into the MMU role, but only for non-nested, in kvm_post_set_cr0()
965 kvm_arch_has_noncoherent_dma(vcpu->kvm) && in kvm_post_set_cr0()
966 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) in kvm_post_set_cr0()
967 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); in kvm_post_set_cr0()
984 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && in kvm_set_cr0()
995 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && in kvm_set_cr0()
1020 if (vcpu->arch.guest_state_protected) in kvm_load_guest_xsave_state()
1025 if (vcpu->arch.xcr0 != host_xcr0) in kvm_load_guest_xsave_state()
1026 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); in kvm_load_guest_xsave_state()
1029 vcpu->arch.ia32_xss != host_xss) in kvm_load_guest_xsave_state()
1030 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); in kvm_load_guest_xsave_state()
1034 vcpu->arch.pkru != vcpu->arch.host_pkru && in kvm_load_guest_xsave_state()
1035 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_guest_xsave_state()
1037 write_pkru(vcpu->arch.pkru); in kvm_load_guest_xsave_state()
1043 if (vcpu->arch.guest_state_protected) in kvm_load_host_xsave_state()
1047 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_host_xsave_state()
1049 vcpu->arch.pkru = rdpkru(); in kvm_load_host_xsave_state()
1050 if (vcpu->arch.pkru != vcpu->arch.host_pkru) in kvm_load_host_xsave_state()
1051 write_pkru(vcpu->arch.host_pkru); in kvm_load_host_xsave_state()
1056 if (vcpu->arch.xcr0 != host_xcr0) in kvm_load_host_xsave_state()
1060 vcpu->arch.ia32_xss != host_xss) in kvm_load_host_xsave_state()
1070 return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; in kvm_guest_supported_xfd()
1077 u64 old_xcr0 = vcpu->arch.xcr0; in __kvm_set_xcr()
1093 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; in __kvm_set_xcr()
1112 vcpu->arch.xcr0 = xcr0; in __kvm_set_xcr()
1137 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) in __kvm_is_valid_cr4()
1156 * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB in kvm_post_set_cr4()
1170 * - CR4.PCIDE is changed from 1 to 0 in kvm_post_set_cr4()
1171 * - CR4.PGE is toggled in kvm_post_set_cr4()
1182 * - CR4.SMEP is changed from 0 to 1 in kvm_post_set_cr4()
1183 * - CR4.PAE is toggled in kvm_post_set_cr4()
1225 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_invalidate_pcid()
1260 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) in kvm_invalidate_pcid()
1263 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); in kvm_invalidate_pcid()
1296 vcpu->arch.cr3 = cr3; in kvm_set_cr3()
1305 * and it's impossible to use a non-zero PCID when PCID is disabled, in kvm_set_cr3()
1322 vcpu->arch.cr8 = cr8; in kvm_set_cr8()
1332 return vcpu->arch.cr8; in kvm_get_cr8()
1340 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { in kvm_update_dr0123()
1342 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_update_dr0123()
1350 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) in kvm_update_dr7()
1351 dr7 = vcpu->arch.guest_debug_dr7; in kvm_update_dr7()
1353 dr7 = vcpu->arch.dr7; in kvm_update_dr7()
1355 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1357 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1375 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_set_dr()
1379 vcpu->arch.db[array_index_nospec(dr, size)] = val; in kvm_set_dr()
1380 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) in kvm_set_dr()
1381 vcpu->arch.eff_db[dr] = val; in kvm_set_dr()
1387 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); in kvm_set_dr()
1393 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; in kvm_set_dr()
1404 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_get_dr()
1408 *val = vcpu->arch.db[array_index_nospec(dr, size)]; in kvm_get_dr()
1412 *val = vcpu->arch.dr6; in kvm_get_dr()
1416 *val = vcpu->arch.dr7; in kvm_get_dr()
1573 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
1585 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
1610 * 10 - MISC_PACKAGE_CTRLS
1611 * 11 - ENERGY_FILTERING_CTL
1612 * 12 - DOITM
1613 * 18 - FB_CLEAR_CTRL
1614 * 21 - XAPIC_DISABLE_STATUS
1615 * 23 - OVERCLOCKING_STATUS
1688 switch (msr->index) { in kvm_get_msr_feature()
1690 msr->data = kvm_get_arch_capabilities(); in kvm_get_msr_feature()
1693 msr->data = kvm_caps.supported_perf_cap; in kvm_get_msr_feature()
1696 rdmsrl_safe(msr->index, &msr->data); in kvm_get_msr_feature()
1759 u64 old_efer = vcpu->arch.efer; in set_efer()
1760 u64 efer = msr_info->data; in set_efer()
1766 if (!msr_info->host_initiated) { in set_efer()
1771 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1776 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1800 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1809 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1811 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1817 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1818 ranges = msr_filter->ranges; in kvm_msr_allowed()
1820 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1827 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1833 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1842 * Returns 0 on success, non-0 otherwise.
1863 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1864 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1865 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1867 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1869 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1871 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1890 * the bits in all other cases. This ensures cross-vendor in __kvm_set_msr()
1922 * Returns 0 on success, non-0 otherwise.
1995 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1996 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1997 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
2003 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
2014 return static_call(kvm_x86_complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
2043 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2046 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2047 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2048 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2049 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2050 vcpu->run->msr.index = index; in kvm_msr_user_space()
2051 vcpu->run->msr.data = data; in kvm_msr_user_space()
2052 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2068 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2069 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2129 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) && in kvm_emulate_monitor_mwait()
2151 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || in kvm_vcpu_exit_request()
2157 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2164 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2171 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2242 return -EINVAL; in do_set_msr()
2277 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2280 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2281 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2282 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2283 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2284 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2285 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2286 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2288 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2289 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2290 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2291 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2292 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2293 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2294 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2296 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2298 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2300 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2344 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_write_wall_clock()
2365 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2367 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2368 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2371 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2374 vcpu->arch.time = system_time; in kvm_write_system_time()
2379 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2382 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2405 shift--; in kvm_get_time_scale()
2450 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2451 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2455 return -1; in set_tsc_khz()
2459 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2464 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2466 return -1; in set_tsc_khz()
2482 return -1; in kvm_set_tsc_khz()
2487 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2488 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2489 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2497 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2509 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2510 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2511 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2512 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2527 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2530 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2531 atomic_read(&vcpu->kvm->online_vcpus)); in kvm_track_tsc_matching()
2541 if (ka->use_master_clock || in kvm_track_tsc_matching()
2542 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) in kvm_track_tsc_matching()
2545 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, in kvm_track_tsc_matching()
2546 atomic_read(&vcpu->kvm->online_vcpus), in kvm_track_tsc_matching()
2547 ka->use_master_clock, gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2554 * The most significant 64-N bits (mult) of ratio represent the
2557 * point number (mult + frac * 2^(-N)).
2580 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); in kvm_compute_l1_tsc_offset()
2582 return target_tsc - tsc; in kvm_compute_l1_tsc_offset()
2587 return vcpu->arch.l1_tsc_offset + in kvm_read_l1_tsc()
2588 kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); in kvm_read_l1_tsc()
2619 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in kvm_vcpu_write_tsc_offset()
2620 vcpu->arch.l1_tsc_offset, in kvm_vcpu_write_tsc_offset()
2623 vcpu->arch.l1_tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2631 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( in kvm_vcpu_write_tsc_offset()
2636 vcpu->arch.tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2643 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2647 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( in kvm_vcpu_write_tsc_multiplier()
2651 vcpu->arch.tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2661 * TSC is marked unstable when we're running on Hyper-V, in kvm_check_tsc_unstable()
2678 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2680 lockdep_assert_held(&kvm->arch.tsc_write_lock); in __kvm_synchronize_tsc()
2686 kvm->arch.last_tsc_nsec = ns; in __kvm_synchronize_tsc()
2687 kvm->arch.last_tsc_write = tsc; in __kvm_synchronize_tsc()
2688 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; in __kvm_synchronize_tsc()
2689 kvm->arch.last_tsc_offset = offset; in __kvm_synchronize_tsc()
2691 vcpu->arch.last_guest_tsc = tsc; in __kvm_synchronize_tsc()
2703 * These values are tracked in kvm->arch.cur_xxx variables. in __kvm_synchronize_tsc()
2705 kvm->arch.cur_tsc_generation++; in __kvm_synchronize_tsc()
2706 kvm->arch.cur_tsc_nsec = ns; in __kvm_synchronize_tsc()
2707 kvm->arch.cur_tsc_write = tsc; in __kvm_synchronize_tsc()
2708 kvm->arch.cur_tsc_offset = offset; in __kvm_synchronize_tsc()
2709 kvm->arch.nr_vcpus_matched_tsc = 0; in __kvm_synchronize_tsc()
2710 } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { in __kvm_synchronize_tsc()
2711 kvm->arch.nr_vcpus_matched_tsc++; in __kvm_synchronize_tsc()
2715 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; in __kvm_synchronize_tsc()
2716 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; in __kvm_synchronize_tsc()
2717 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; in __kvm_synchronize_tsc()
2724 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2730 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2733 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2735 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2738 * detection of vcpu initialization -- need to sync in kvm_synchronize_tsc()
2744 u64 tsc_exp = kvm->arch.last_tsc_write + in kvm_synchronize_tsc()
2746 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; in kvm_synchronize_tsc()
2764 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2766 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2776 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2782 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2788 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2791 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2823 switch (clock->vclock_mode) { in vgettsc()
2829 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2830 clock->mask; in vgettsc()
2839 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2840 clock->mask; in vgettsc()
2849 return v * clock->mult; in vgettsc()
2860 seq = read_seqcount_begin(&gtod->seq); in do_monotonic_raw()
2861 ns = gtod->raw_clock.base_cycles; in do_monotonic_raw()
2862 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode); in do_monotonic_raw()
2863 ns >>= gtod->raw_clock.shift; in do_monotonic_raw()
2864 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_monotonic_raw()
2865 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_monotonic_raw()
2879 seq = read_seqcount_begin(&gtod->seq); in do_realtime()
2880 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2881 ns = gtod->clock.base_cycles; in do_realtime()
2882 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_realtime()
2883 ns >>= gtod->clock.shift; in do_realtime()
2884 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_realtime()
2886 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2887 ts->tv_nsec = ns; in do_realtime()
2931 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2932 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2933 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2937 * - ret0 < ret1
2938 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2940 * - 0 < N - M => M < N
2959 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
2963 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
2964 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
2965 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
2972 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
2973 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
2975 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
2976 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
2977 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
2979 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
2983 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
2995 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
2996 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3009 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3013 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3014 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3033 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3047 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3050 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3056 data->flags = 0; in __get_kvmclock()
3057 if (ka->use_master_clock && in __get_kvmclock()
3062 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3063 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3064 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3067 data->host_tsc = rdtsc(); in __get_kvmclock()
3069 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3070 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3071 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3075 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3077 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3085 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3089 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3091 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3106 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_setup_guest_pvclock()
3110 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3112 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3117 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3120 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3129 guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3133 vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3135 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_setup_guest_pvclock()
3136 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; in kvm_setup_guest_pvclock()
3137 vcpu->pvclock_set_guest_stopped_request = false; in kvm_setup_guest_pvclock()
3140 memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); in kvm_setup_guest_pvclock()
3143 guest_hv_clock->version = ++vcpu->hv_clock.version; in kvm_setup_guest_pvclock()
3145 mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); in kvm_setup_guest_pvclock()
3146 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3148 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); in kvm_setup_guest_pvclock()
3155 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3156 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3170 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3171 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3173 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3174 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3176 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3203 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3206 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3217 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3219 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3221 &vcpu->hv_clock.tsc_shift, in kvm_guest_time_update()
3222 &vcpu->hv_clock.tsc_to_system_mul); in kvm_guest_time_update()
3223 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3227 vcpu->hv_clock.tsc_timestamp = tsc_timestamp; in kvm_guest_time_update()
3228 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3229 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3236 vcpu->hv_clock.flags = pvclock_flags; in kvm_guest_time_update()
3238 if (vcpu->pv_time.active) in kvm_guest_time_update()
3239 kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0); in kvm_guest_time_update()
3240 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3241 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3243 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3244 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0); in kvm_guest_time_update()
3245 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); in kvm_guest_time_update()
3251 * vcpu->cpu migration, should not allow system_timestamp from
3257 * We need to rate-limit these requests though, as they can
3260 * by the delay we use to rate-limit the updates.
3282 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3285 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3289 #define KVMCLOCK_SYNC_PERIOD (300 * HZ)
3301 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3302 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3323 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3330 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3332 u32 msr = msr_info->index; in set_msr_mce()
3333 u64 data = msr_info->data; in set_msr_mce()
3338 vcpu->arch.mcg_status = data; in set_msr_mce()
3342 (data || !msr_info->host_initiated)) in set_msr_mce()
3346 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3348 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3349 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3353 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3358 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3359 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3360 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3362 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3363 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3375 * single-bit ECC data errors. in set_msr_mce()
3383 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3386 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3390 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3391 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3392 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3404 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3426 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3434 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3438 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3439 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3448 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3455 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3457 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3464 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3465 vcpu->arch.time = 0; in kvmclock_reset()
3470 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3479 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3495 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3504 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3512 * prior before nested VM-Enter/VM-Exit.
3526 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3529 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3533 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3538 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3541 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3544 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3546 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3547 gpa != ghc->gpa || in record_steal_time()
3548 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3550 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3552 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3553 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3557 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3564 int err = -EFAULT; in record_steal_time()
3575 "+m" (st->preempted)); in record_steal_time()
3581 vcpu->arch.st.preempted = 0; in record_steal_time()
3583 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3594 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3595 vcpu->arch.st.preempted = 0; in record_steal_time()
3598 unsafe_get_user(version, &st->version, out); in record_steal_time()
3603 unsafe_put_user(version, &st->version, out); in record_steal_time()
3607 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3608 steal += current->sched_info.run_delay - in record_steal_time()
3609 vcpu->arch.st.last_steal; in record_steal_time()
3610 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3611 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3614 unsafe_put_user(version, &st->version, out); in record_steal_time()
3619 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3636 u32 msr = msr_info->index; in kvm_set_msr_common()
3637 u64 data = msr_info->data; in kvm_set_msr_common()
3639 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) in kvm_set_msr_common()
3654 if (msr_info->host_initiated) in kvm_set_msr_common()
3655 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3658 if (!msr_info->host_initiated) in kvm_set_msr_common()
3660 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3663 if (!msr_info->host_initiated) in kvm_set_msr_common()
3673 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3676 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3680 if (!msr_info->host_initiated && !guest_has_pred_cmd_msr(vcpu)) in kvm_set_msr_common()
3691 if (!msr_info->host_initiated && in kvm_set_msr_common()
3711 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3727 vcpu->arch.pat = data; in kvm_set_msr_common()
3741 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3742 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3749 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3753 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3755 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3765 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3769 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3772 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3777 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3779 vcpu->arch.smbase = data; in kvm_set_msr_common()
3782 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3785 if (msr_info->host_initiated) { in kvm_set_msr_common()
3788 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3790 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3794 if (!msr_info->host_initiated && in kvm_set_msr_common()
3804 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3808 if (!msr_info->host_initiated) in kvm_set_msr_common()
3810 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3816 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3817 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3823 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3824 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3830 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
3836 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
3856 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
3870 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
3891 if (data & (-1ULL << 1)) in kvm_set_msr_common()
3894 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
3899 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
3900 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
3917 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
3934 msr_info->host_initiated); in kvm_set_msr_common()
3936 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
3944 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
3949 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
3952 if (!msr_info->host_initiated || in kvm_set_msr_common()
3956 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
3963 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
3967 if (!msr_info->host_initiated && in kvm_set_msr_common()
3974 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
3977 if (!msr_info->host_initiated && in kvm_set_msr_common()
3984 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
3993 * as to-be-saved, even if an MSRs isn't fully supported. in kvm_set_msr_common()
3995 if (msr_info->host_initiated && !data && in kvm_set_msr_common()
4008 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4018 data = vcpu->arch.mcg_cap; in get_msr_mce()
4023 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4026 data = vcpu->arch.mcg_status; in get_msr_mce()
4028 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4029 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4035 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4036 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4037 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4039 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4040 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4044 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4045 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4046 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4057 switch (msr_info->index) { in kvm_get_msr_common()
4080 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4087 msr_info->data = 0; in kvm_get_msr_common()
4093 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4095 msr_info->data = 0; in kvm_get_msr_common()
4098 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4101 if (!msr_info->host_initiated && in kvm_get_msr_common()
4104 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4107 if (!msr_info->host_initiated && in kvm_get_msr_common()
4110 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4113 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4122 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4127 if (msr_info->host_initiated) { in kvm_get_msr_common()
4128 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4129 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4131 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4132 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4135 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4139 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4144 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4146 msr_info->data = 3; in kvm_get_msr_common()
4160 msr_info->data = 1 << 24; in kvm_get_msr_common()
4163 msr_info->data = kvm_get_apic_base(vcpu); in kvm_get_msr_common()
4166 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4168 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4171 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4174 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4177 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4179 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4182 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4186 msr_info->data = 1000ULL; in kvm_get_msr_common()
4188 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4191 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4197 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4203 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4209 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4215 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4221 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4227 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4233 msr_info->data = 0; in kvm_get_msr_common()
4239 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4245 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4251 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4258 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4259 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4260 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4261 msr_info->host_initiated); in kvm_get_msr_common()
4263 if (!msr_info->host_initiated && in kvm_get_msr_common()
4266 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4270 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4278 msr_info->data = 0x20000000; in kvm_get_msr_common()
4291 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4292 msr_info->host_initiated); in kvm_get_msr_common()
4304 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4309 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4314 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4317 if (!msr_info->host_initiated && in kvm_get_msr_common()
4318 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4320 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4323 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4326 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4330 if (!msr_info->host_initiated && in kvm_get_msr_common()
4334 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4337 if (!msr_info->host_initiated && in kvm_get_msr_common()
4341 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4345 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4350 * to-be-saved, even if an MSR isn't fully supported. in kvm_get_msr_common()
4352 if (msr_info->host_initiated && in kvm_get_msr_common()
4353 kvm_is_msr_to_save(msr_info->index)) { in kvm_get_msr_common()
4354 msr_info->data = 0; in kvm_get_msr_common()
4376 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4398 r = -EFAULT; in msr_io()
4402 r = -E2BIG; in msr_io()
4407 entries = memdup_user(user_msrs->entries, size); in msr_io()
4415 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4416 r = -EFAULT; in msr_io()
4436 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4440 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4444 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4451 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) in kvm_vm_ioctl_check_extension() argument
4455 switch (ext) { in kvm_vm_ioctl_check_extension()
4604 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4605 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4611 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4649 void __user *uaddr = (void __user*)(unsigned long)attr->addr; in kvm_get_attr_addr()
4651 if ((u64)(unsigned long)uaddr != attr->addr) in kvm_get_attr_addr()
4652 return ERR_PTR_USR(-EFAULT); in kvm_get_attr_addr()
4660 if (attr->group) in kvm_x86_dev_get_attr()
4661 return -ENXIO; in kvm_x86_dev_get_attr()
4666 switch (attr->attr) { in kvm_x86_dev_get_attr()
4669 return -EFAULT; in kvm_x86_dev_get_attr()
4672 return -ENXIO; in kvm_x86_dev_get_attr()
4678 if (attr->group) in kvm_x86_dev_has_attr()
4679 return -ENXIO; in kvm_x86_dev_has_attr()
4681 switch (attr->attr) { in kvm_x86_dev_has_attr()
4685 return -ENXIO; in kvm_x86_dev_has_attr()
4701 r = -EFAULT; in kvm_arch_dev_ioctl()
4708 r = -E2BIG; in kvm_arch_dev_ioctl()
4711 r = -EFAULT; in kvm_arch_dev_ioctl()
4712 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4715 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4727 r = -EFAULT; in kvm_arch_dev_ioctl()
4731 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4736 r = -EFAULT; in kvm_arch_dev_ioctl()
4743 r = -EFAULT; in kvm_arch_dev_ioctl()
4754 r = -EFAULT; in kvm_arch_dev_ioctl()
4761 r = -E2BIG; in kvm_arch_dev_ioctl()
4764 r = -EFAULT; in kvm_arch_dev_ioctl()
4765 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4779 r = -EFAULT; in kvm_arch_dev_ioctl()
4787 r = -EFAULT; in kvm_arch_dev_ioctl()
4794 r = -EINVAL; in kvm_arch_dev_ioctl()
4808 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4816 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4817 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4818 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
4825 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
4828 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
4829 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
4830 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
4834 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
4835 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
4836 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
4842 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
4844 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
4852 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
4854 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
4856 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4858 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
4866 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
4870 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
4873 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
4877 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
4879 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
4880 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
4884 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
4885 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
4888 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
4892 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
4895 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
4897 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
4898 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
4899 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
4902 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
4903 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
4905 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
4906 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
4908 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
4915 if (vcpu->preempted) { in kvm_arch_vcpu_put()
4916 if (!vcpu->arch.guest_state_protected) in kvm_arch_vcpu_put()
4917 vcpu->arch.preempted_in_kernel = !static_call(kvm_x86_get_cpl)(vcpu); in kvm_arch_vcpu_put()
4923 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
4924 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
4928 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
4932 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
4979 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
4990 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
4991 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
4993 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
4994 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5000 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5001 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5003 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5004 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5006 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5007 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5009 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5024 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5025 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5026 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5036 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5042 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5045 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5048 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5050 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5063 * - none of the bits for Machine Check Exceptions are set
5064 * - both the VAL (valid) and UC (uncorrectable) bits are set
5065 * MCI_STATUS_PCC - Processor Context Corrupted
5066 * MCI_STATUS_S - Signaled as a Machine Check Exception
5067 * MCI_STATUS_AR - Software recoverable Action Required
5071 return !mce->mcg_status && in is_ucna()
5072 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5073 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5074 (mce->status & MCI_STATUS_UC); in is_ucna()
5079 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5081 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5082 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5083 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5084 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5087 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5091 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5099 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5101 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5103 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5104 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5106 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5115 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5116 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5122 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5124 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5125 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5131 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5132 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5133 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5134 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5135 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5140 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5141 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5142 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5143 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5164 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5165 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5168 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5169 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5170 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5171 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5173 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5178 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5183 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5184 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5195 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5196 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5197 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5203 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5204 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5206 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5207 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5208 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5209 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5210 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5212 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5213 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5214 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5215 events->interrupt.shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5217 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5218 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5219 events->nmi.masked = static_call(kvm_x86_get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5221 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5224 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5225 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5226 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5227 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5229 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5231 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5234 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5235 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5236 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5237 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5238 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5245 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5251 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5253 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5254 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5255 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5256 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5257 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5259 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5261 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5262 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5265 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5266 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5267 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5270 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5271 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5272 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5273 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5279 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5280 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5283 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5285 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5287 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5289 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5290 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5291 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5292 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5293 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5294 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5295 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5297 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5298 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5299 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5300 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5302 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5304 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5305 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5306 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5307 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5308 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5311 static_call(kvm_x86_set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5313 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5315 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5317 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5319 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5321 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5324 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5326 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5327 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5328 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5330 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5334 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5335 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5336 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5340 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5341 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5343 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5347 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5348 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5349 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5350 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5367 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5369 dbgregs->dr6 = val; in kvm_vcpu_ioctl_x86_get_debugregs()
5370 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5376 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5377 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5379 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5380 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5381 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5382 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5384 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); in kvm_vcpu_ioctl_x86_set_debugregs()
5386 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5387 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5409 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5412 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5415 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5416 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5422 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5423 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5429 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5432 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5433 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5435 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5442 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5446 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5447 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5448 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5449 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5458 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5460 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5461 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5463 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5465 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5467 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5471 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5483 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5484 return -EINVAL; in kvm_set_guest_paused()
5485 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5495 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5500 r = -ENXIO; in kvm_arch_tsc_has_attr()
5515 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5517 r = -EFAULT; in kvm_arch_tsc_get_attr()
5518 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5523 r = -ENXIO; in kvm_arch_tsc_get_attr()
5533 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5539 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5545 r = -EFAULT; in kvm_arch_tsc_set_attr()
5549 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5551 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5552 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5553 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5555 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5559 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5565 r = -ENXIO; in kvm_arch_tsc_set_attr()
5579 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5582 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5606 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5607 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5609 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5611 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5612 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5616 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5617 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5618 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5621 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5622 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5623 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5625 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5628 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5633 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5638 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5641 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5642 if (vcpu->arch.pv_cpuid.enforce) in kvm_vcpu_ioctl_enable_cap()
5647 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5654 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5670 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5676 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5682 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5689 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5704 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5722 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5725 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5732 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5736 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5743 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5747 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5750 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5757 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5759 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5763 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5765 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5771 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5777 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5787 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5790 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5793 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5795 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5801 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5810 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5821 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5830 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5844 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5854 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5863 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5864 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
5868 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5874 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5881 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
5894 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
5897 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5903 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5913 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5919 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5939 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5955 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
5965 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5975 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5976 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
5979 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
5980 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5981 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
5984 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
5990 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
5991 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5993 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6005 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6006 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6009 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6013 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6028 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6029 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6030 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6040 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6045 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6051 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6060 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6064 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6086 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6104 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6105 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6120 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6122 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6125 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6127 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6133 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6137 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6139 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6143 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6147 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6150 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6158 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6162 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6164 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6165 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6167 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6170 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6171 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6173 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6176 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6179 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6188 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6190 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6192 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6193 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6194 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6201 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6203 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6204 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6206 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6207 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6213 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6214 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6215 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6216 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6217 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6218 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6227 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6229 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6230 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6231 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6234 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6235 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6236 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6238 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6240 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6247 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6249 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6253 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6254 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6255 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6266 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6267 * VM-Exit. in kvm_arch_sync_dirty_log()
6280 return -ENXIO; in kvm_vm_ioctl_irq_line()
6282 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6283 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6293 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6294 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6296 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6298 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6299 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6303 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6307 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6308 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6309 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6311 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6314 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6321 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6322 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6326 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6330 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6331 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6334 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6335 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6336 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6337 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6342 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6343 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) in kvm_vm_ioctl_enable_cap()
6346 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6347 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6349 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6354 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6357 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && in kvm_vm_ioctl_enable_cap()
6359 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6360 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6361 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6362 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6363 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6369 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6373 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6377 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6381 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6382 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6384 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6388 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6389 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6392 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6393 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6397 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6398 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6405 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6412 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6414 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6419 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6423 r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6426 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6430 r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6433 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6434 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6437 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6441 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6442 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6444 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6448 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6449 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6452 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6453 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6454 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6457 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6460 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6461 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6464 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6465 if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6467 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6468 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6471 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6474 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6475 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6479 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6481 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6482 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6483 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6484 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6487 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6490 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6504 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6508 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6511 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6512 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6513 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6516 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6519 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6533 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6544 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6545 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6556 if (!user_range->nmsrs) in kvm_add_msr_filter()
6559 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6560 return -EINVAL; in kvm_add_msr_filter()
6562 if (!user_range->flags) in kvm_add_msr_filter()
6563 return -EINVAL; in kvm_add_msr_filter()
6565 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6567 return -EINVAL; in kvm_add_msr_filter()
6569 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6573 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6574 .flags = user_range->flags, in kvm_add_msr_filter()
6575 .base = user_range->base, in kvm_add_msr_filter()
6576 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6580 msr_filter->count++; in kvm_add_msr_filter()
6593 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6594 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6596 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6597 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6599 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6601 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6605 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6607 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6608 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6615 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6616 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6617 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6618 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6619 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6648 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6649 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6660 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6668 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6669 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6670 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6671 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6691 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6693 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6699 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6703 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6726 return -EFAULT; in kvm_vm_ioctl_get_clock()
6733 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6738 return -EFAULT; in kvm_vm_ioctl_set_clock()
6745 return -EINVAL; in kvm_vm_ioctl_set_clock()
6765 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
6768 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
6769 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
6772 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
6779 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
6781 int r = -ENOTTY; in kvm_arch_vm_ioctl()
6783 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
6800 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6801 r = -EINVAL; in kvm_arch_vm_ioctl()
6802 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6804 r = -EFAULT; in kvm_arch_vm_ioctl()
6809 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6816 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6818 r = -EEXIST; in kvm_arch_vm_ioctl()
6822 r = -EINVAL; in kvm_arch_vm_ioctl()
6823 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6842 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
6844 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
6847 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6854 r = -EFAULT; in kvm_arch_vm_ioctl()
6859 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6860 r = -EEXIST; in kvm_arch_vm_ioctl()
6861 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
6863 r = -ENOMEM; in kvm_arch_vm_ioctl()
6864 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
6865 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
6868 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6880 r = -ENXIO; in kvm_arch_vm_ioctl()
6886 r = -EFAULT; in kvm_arch_vm_ioctl()
6904 r = -ENXIO; in kvm_arch_vm_ioctl()
6913 r = -EFAULT; in kvm_arch_vm_ioctl()
6916 r = -ENXIO; in kvm_arch_vm_ioctl()
6917 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6922 r = -EFAULT; in kvm_arch_vm_ioctl()
6929 r = -EFAULT; in kvm_arch_vm_ioctl()
6932 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6933 r = -ENXIO; in kvm_arch_vm_ioctl()
6934 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6938 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6942 r = -ENXIO; in kvm_arch_vm_ioctl()
6943 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6948 r = -EFAULT; in kvm_arch_vm_ioctl()
6955 r = -EFAULT; in kvm_arch_vm_ioctl()
6958 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6959 r = -ENXIO; in kvm_arch_vm_ioctl()
6960 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6964 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6969 r = -EFAULT; in kvm_arch_vm_ioctl()
6972 r = -ENXIO; in kvm_arch_vm_ioctl()
6973 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
6980 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
6981 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
6982 r = -EBUSY; in kvm_arch_vm_ioctl()
6984 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
6985 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
6990 r = -EFAULT; in kvm_arch_vm_ioctl()
6999 r = -EFAULT; in kvm_arch_vm_ioctl()
7004 r = -EFAULT; in kvm_arch_vm_ioctl()
7010 r = -EFAULT; in kvm_arch_vm_ioctl()
7019 r = -EFAULT; in kvm_arch_vm_ioctl()
7035 r = -EINVAL; in kvm_arch_vm_ioctl()
7045 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7051 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7055 r = -ENOTTY; in kvm_arch_vm_ioctl()
7065 r = -EFAULT; in kvm_arch_vm_ioctl()
7069 r = -ENOTTY; in kvm_arch_vm_ioctl()
7079 r = -EFAULT; in kvm_arch_vm_ioctl()
7083 r = -ENOTTY; in kvm_arch_vm_ioctl()
7093 r = -EFAULT; in kvm_arch_vm_ioctl()
7107 return -EFAULT; in kvm_arch_vm_ioctl()
7113 r = -ENOTTY; in kvm_arch_vm_ioctl()
7175 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7180 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7185 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7190 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7258 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7263 len -= n; in vcpu_mmio_write()
7278 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7285 len -= n; in vcpu_mmio_read()
7307 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7312 /* NPT walks are always user-walks */ in translate_nested_gpa()
7314 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7322 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7325 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7332 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7336 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7344 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7346 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7353 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7358 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7359 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7360 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7372 bytes -= toread; in kvm_read_guest_virt_helper()
7386 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7392 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7397 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7399 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7445 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7450 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7451 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7452 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7463 bytes -= towrite; in kvm_write_guest_virt_helper()
7491 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7549 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7559 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7560 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7561 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7562 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7567 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7570 return -1; in vcpu_mmio_gva_to_gpa()
7601 if (vcpu->mmio_read_completed) { in read_prepare()
7603 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7604 vcpu->mmio_read_completed = 0; in read_prepare()
7639 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7641 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7667 bool write = ops->write; in emulator_read_write_onepage()
7669 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7678 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7679 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7680 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7688 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7694 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7699 bytes -= handled; in emulator_read_write_onepage()
7702 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7703 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7704 frag->gpa = gpa; in emulator_read_write_onepage()
7705 frag->data = val; in emulator_read_write_onepage()
7706 frag->len = bytes; in emulator_read_write_onepage()
7720 if (ops->read_write_prepare && in emulator_read_write()
7721 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7724 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7727 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7730 now = -addr & ~PAGE_MASK; in emulator_read_write()
7737 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
7740 bytes -= now; in emulator_read_write()
7748 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
7751 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
7753 vcpu->mmio_needed = 1; in emulator_read_write()
7754 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
7756 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
7757 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
7758 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
7759 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
7761 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
7801 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
7815 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
7819 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
7877 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
7893 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
7902 vcpu->arch.pio.port = port; in emulator_pio_in_out()
7903 vcpu->arch.pio.in = in; in emulator_pio_in_out()
7904 vcpu->arch.pio.count = count; in emulator_pio_in_out()
7905 vcpu->arch.pio.size = size; in emulator_pio_in_out()
7908 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
7910 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
7912 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
7913 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
7914 vcpu->run->io.size = size; in emulator_pio_in_out()
7915 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
7916 vcpu->run->io.count = count; in emulator_pio_in_out()
7917 vcpu->run->io.port = port; in emulator_pio_in_out()
7933 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
7934 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
7935 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
7936 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
7937 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
7945 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
7993 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
7994 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
7997 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8032 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8045 value = vcpu->arch.cr2; in emulator_get_cr()
8074 vcpu->arch.cr2 = val; in emulator_set_cr()
8087 res = -1; in emulator_set_cr()
8148 desc->type = var.type; in emulator_get_segment()
8149 desc->s = var.s; in emulator_get_segment()
8150 desc->dpl = var.dpl; in emulator_get_segment()
8151 desc->p = var.present; in emulator_get_segment()
8152 desc->avl = var.avl; in emulator_get_segment()
8153 desc->l = var.l; in emulator_get_segment()
8154 desc->d = var.db; in emulator_get_segment()
8155 desc->g = var.g; in emulator_get_segment()
8173 if (desc->g) in emulator_set_segment()
8175 var.type = desc->type; in emulator_set_segment()
8176 var.dpl = desc->dpl; in emulator_set_segment()
8177 var.db = desc->d; in emulator_set_segment()
8178 var.s = desc->s; in emulator_set_segment()
8179 var.l = desc->l; in emulator_set_segment()
8180 var.g = desc->g; in emulator_set_segment()
8181 var.avl = desc->avl; in emulator_set_segment()
8182 var.present = desc->p; in emulator_set_segment()
8247 return -EINVAL; in emulator_check_pmc()
8258 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8266 &ctxt->exception); in emulator_intercept()
8336 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8338 if (!kvm->vm_bugged) in emulator_vm_bugged()
8409 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8411 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8412 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8413 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8414 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8415 ctxt->exception.error_code); in inject_emulated_exception()
8417 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8430 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8431 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8432 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8439 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8444 ctxt->gpa_available = false; in init_emulate_ctxt()
8445 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8446 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8448 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8449 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8450 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8454 ctxt->interruptibility = 0; in init_emulate_ctxt()
8455 ctxt->have_exception = false; in init_emulate_ctxt()
8456 ctxt->exception.vector = -1; in init_emulate_ctxt()
8457 ctxt->perm_ok = false; in init_emulate_ctxt()
8460 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8465 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8470 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8471 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8472 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8478 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8479 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8480 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8488 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8502 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8503 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8515 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8518 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8519 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8521 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8523 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8524 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8525 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8526 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8529 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8530 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8533 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8538 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8540 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8541 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8559 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8561 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8569 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8598 if (!vcpu->arch.mmu->root_role.direct) { in reexecute_instruction()
8616 * retry instruction -> write #PF -> emulation fail -> retry in reexecute_instruction()
8617 * instruction -> ... in reexecute_instruction()
8619 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8630 /* The instructions are well-emulated on direct mmu. */ in reexecute_instruction()
8631 if (vcpu->arch.mmu->root_role.direct) { in reexecute_instruction()
8634 write_lock(&vcpu->kvm->mmu_lock); in reexecute_instruction()
8635 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; in reexecute_instruction()
8636 write_unlock(&vcpu->kvm->mmu_lock); in reexecute_instruction()
8639 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8646 * and it failed try to unshadow page and re-enter the in reexecute_instruction()
8649 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in reexecute_instruction()
8665 last_retry_eip = vcpu->arch.last_retry_eip; in retry_instruction()
8666 last_retry_addr = vcpu->arch.last_retry_addr; in retry_instruction()
8669 * If the emulation is caused by #PF and it is non-page_table in retry_instruction()
8670 * writing instruction, it means the VM-EXIT is caused by shadow in retry_instruction()
8674 * Note: if the guest uses a non-page-table modifying instruction in retry_instruction()
8681 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; in retry_instruction()
8693 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) in retry_instruction()
8696 vcpu->arch.last_retry_eip = ctxt->eip; in retry_instruction()
8697 vcpu->arch.last_retry_addr = cr2_or_gpa; in retry_instruction()
8699 if (!vcpu->arch.mmu->root_role.direct) in retry_instruction()
8702 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); in retry_instruction()
8727 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8729 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8730 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8731 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8732 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8733 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8805 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8806 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8807 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
8810 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
8811 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
8814 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
8815 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
8816 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
8817 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
8823 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
8827 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
8828 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
8842 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
8844 switch (ctxt->b) { in is_vmware_backdoor_opcode()
8861 switch (ctxt->b) { in is_vmware_backdoor_opcode()
8874 * (and wrong) when emulating on an intercepted fault-like exception[*], as
8884 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
8892 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
8902 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
8908 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
8915 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
8933 if (ctxt->have_exception && in x86_emulate_instruction()
8936 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
8939 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
8940 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
8958 * injecting single-step #DBs. in x86_emulate_instruction()
8961 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
8962 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
8964 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
8971 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
8972 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
8973 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
8982 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
8983 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
8990 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
8993 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
8994 ctxt->gpa_available = true; in x86_emulate_instruction()
8995 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
8999 ctxt->exception.address = 0; in x86_emulate_instruction()
9014 if (ctxt->have_exception) { in x86_emulate_instruction()
9015 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9016 vcpu->mmio_needed = false; in x86_emulate_instruction()
9019 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9020 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9021 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9022 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9025 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9028 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9029 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9031 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9034 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9035 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9046 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9047 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9050 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9052 * of which are fault-like. in x86_emulate_instruction()
9054 if (!ctxt->have_exception || in x86_emulate_instruction()
9055 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9057 if (ctxt->is_branch) in x86_emulate_instruction()
9059 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9060 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9063 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9072 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9075 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9095 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9101 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9103 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9123 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9124 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9128 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9129 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9139 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9141 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9142 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9147 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9170 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9171 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9196 struct cpufreq_freqs *freq = data; in tsc_khz_changed() local
9202 khz = freq->new; in tsc_khz_changed()
9223 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9240 static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) in __kvmclock_cpufreq_notifier() argument
9286 smp_call_function_single(cpu, tsc_khz_changed, freq, 1); in __kvmclock_cpufreq_notifier()
9291 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9294 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9300 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9313 smp_call_function_single(cpu, tsc_khz_changed, freq, 1); in __kvmclock_cpufreq_notifier()
9320 struct cpufreq_freqs *freq = data; in kvmclock_cpufreq_notifier() local
9323 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9325 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9328 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9329 __kvmclock_cpufreq_notifier(freq, cpu); in kvmclock_cpufreq_notifier()
9356 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9357 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9365 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", in kvm_timer_init()
9415 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9428 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9438 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9441 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9459 return -EIO; in kvm_x86_check_processor_compatibility()
9476 return -EEXIST; in __kvm_x86_vendor_init()
9486 return -EOPNOTSUPP; in __kvm_x86_vendor_init()
9491 return -EOPNOTSUPP; in __kvm_x86_vendor_init()
9504 return -EIO; in __kvm_x86_vendor_init()
9510 return -ENOMEM; in __kvm_x86_vendor_init()
9516 r = -ENOMEM; in __kvm_x86_vendor_init()
9535 kvm_init_pmu_capability(ops->pmu_ops); in __kvm_x86_vendor_init()
9540 r = ops->hardware_setup(); in __kvm_x86_vendor_init()
9559 if (pi_inject_timer == -1) in __kvm_x86_vendor_init()
9568 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in __kvm_x86_vendor_init()
9654 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
9659 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
9661 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
9664 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
9679 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
9705 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9711 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9712 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9715 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9724 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9726 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9735 * @apicid - apicid of vcpu to be kicked.
9755 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9761 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9781 unsigned long *inhibits = &kvm->arch.apicv_inhibit_reasons; in kvm_apicv_init()
9783 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9797 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9803 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9805 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9806 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9810 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9820 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
9828 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
9833 ++vcpu->stat.hypercalls; in complete_hypercall_exit()
9842 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
9866 ret = -KVM_EPERM; in kvm_emulate_hypercall()
9870 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
9880 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in kvm_emulate_hypercall()
9893 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in kvm_emulate_hypercall()
9905 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
9906 if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) in kvm_emulate_hypercall()
9911 ret = -KVM_EINVAL; in kvm_emulate_hypercall()
9915 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in kvm_emulate_hypercall()
9916 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in kvm_emulate_hypercall()
9917 vcpu->run->hypercall.args[0] = gpa; in kvm_emulate_hypercall()
9918 vcpu->run->hypercall.args[1] = npages; in kvm_emulate_hypercall()
9919 vcpu->run->hypercall.args[2] = attrs; in kvm_emulate_hypercall()
9920 vcpu->run->hypercall.flags = 0; in kvm_emulate_hypercall()
9922 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in kvm_emulate_hypercall()
9924 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in kvm_emulate_hypercall()
9925 vcpu->arch.complete_userspace_io = complete_hypercall_exit; in kvm_emulate_hypercall()
9929 ret = -KVM_ENOSYS; in kvm_emulate_hypercall()
9937 ++vcpu->stat.hypercalls; in kvm_emulate_hypercall()
9952 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
9953 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
9954 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
9955 ctxt->have_exception = true; in emulator_fix_hypercall()
9962 &ctxt->exception); in emulator_fix_hypercall()
9967 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
9968 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
9971 /* Called within kvm->srcu read side. */
9974 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
9976 kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu); in post_kvm_run_save()
9977 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
9978 kvm_run->apic_base = kvm_get_apic_base(vcpu); in post_kvm_run_save()
9980 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
9981 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
9985 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
9998 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10001 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10004 max_irr = -1; in update_cr8_intercept()
10006 if (max_irr != -1) in update_cr8_intercept()
10018 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10022 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10031 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10034 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10036 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10037 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10038 vcpu->arch.exception.error_code, in kvm_inject_exception()
10039 vcpu->arch.exception.injected); in kvm_inject_exception()
10049 * injected as part of a previous VM-Enter, but weren't successfully delivered
10050 * and need to be re-injected.
10055 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10056 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10061 * instruction boundaries for asynchronous events. However, because VM-Exits
10067 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10090 * Process nested events first, as nested VM-Exit supercedes event in kvm_check_and_inject_events()
10091 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10092 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10100 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10104 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10113 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10115 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10118 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10122 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10126 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10128 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10132 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10133 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10136 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10137 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10141 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10144 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10145 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10152 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10153 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10154 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10156 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10157 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10161 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10166 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10168 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10169 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10170 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10175 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10177 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10181 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10182 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10183 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10184 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10191 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10192 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10198 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10203 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10204 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10210 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10213 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10214 r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10218 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10219 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10227 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10228 r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10232 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10233 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10238 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10243 r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY; in kvm_check_and_inject_events()
10249 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10260 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10261 kvm_x86_ops.nested_ops->has_events(vcpu, true)) in kvm_check_and_inject_events()
10266 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10268 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10276 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10277 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10281 if (r == -EBUSY) { in kvm_check_and_inject_events()
10302 if (static_call(kvm_x86_get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10309 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10312 limit--; in process_nmi()
10314 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10315 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10317 if (vcpu->arch.nmi_pending && in process_nmi()
10319 vcpu->arch.nmi_pending--; in process_nmi()
10321 if (vcpu->arch.nmi_pending) in process_nmi()
10328 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10345 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10351 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10358 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10361 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10371 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10376 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10396 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10408 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10413 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10431 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10434 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10437 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10440 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10450 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10452 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10461 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10465 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10466 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10467 else if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10468 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10471 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10480 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10485 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10486 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10492 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10510 smp_send_reschedule(vcpu->cpu); in __kvm_request_immediate_exit()
10515 * Called within kvm->srcu read side.
10532 r = -EIO; in vcpu_enter_guest()
10542 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10552 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10576 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10577 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10586 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10592 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10595 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10596 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10603 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10620 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10621 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10622 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10623 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10624 vcpu->run->eoi.vector = in vcpu_enter_guest()
10625 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10637 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10638 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10639 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10644 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10645 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10646 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10653 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10654 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10661 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10662 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10679 ++vcpu->stat.req_event; in vcpu_enter_guest()
10685 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10720 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10721 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10726 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10729 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10750 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10768 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10769 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10771 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10773 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10774 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10775 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10776 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10778 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) in vcpu_enter_guest()
10779 static_call(kvm_x86_set_dr6)(vcpu, vcpu->arch.dr6); in vcpu_enter_guest()
10790 * per-VM state, and responsing vCPUs must wait for the update in vcpu_enter_guest()
10808 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
10809 ++vcpu->stat.exits; in vcpu_enter_guest()
10818 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
10819 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
10835 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
10836 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
10838 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10843 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
10846 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
10851 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10856 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
10863 ++vcpu->stat.exits; in vcpu_enter_guest()
10889 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
10892 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
10902 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
10908 /* Called within kvm->srcu read side. */
10915 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
10918 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
10926 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
10947 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
10957 switch(vcpu->arch.mp_state) { in vcpu_block()
10960 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
10961 vcpu->arch.mp_state = in vcpu_block()
10965 vcpu->arch.apf.halted = false; in vcpu_block()
10978 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
10979 !vcpu->arch.apf.halted); in kvm_vcpu_running()
10982 /* Called within kvm->srcu read side. */
10987 vcpu->arch.l1tf_flush_l1d = true; in vcpu_run()
10996 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11016 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11017 ++vcpu->stat.request_irq_exits; in vcpu_run()
11040 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11065 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11069 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11072 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11073 len = min(8u, frag->len); in complete_emulated_mmio()
11074 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11075 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11077 if (frag->len <= 8) { in complete_emulated_mmio()
11080 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11083 frag->data += len; in complete_emulated_mmio()
11084 frag->gpa += len; in complete_emulated_mmio()
11085 frag->len -= len; in complete_emulated_mmio()
11088 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11089 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11091 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11092 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11094 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11098 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11099 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11100 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11101 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11102 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11103 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11104 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11111 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11112 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11119 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11120 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11126 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11127 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11132 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11136 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11137 if (kvm_run->immediate_exit) { in kvm_arch_vcpu_ioctl_run()
11138 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11160 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11162 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11163 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11164 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11169 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || in kvm_arch_vcpu_ioctl_run()
11170 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { in kvm_arch_vcpu_ioctl_run()
11171 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11175 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11181 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11183 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11184 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11191 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11193 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11194 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11195 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11196 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11197 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11198 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11199 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11200 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11202 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11204 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11205 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11206 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11211 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11212 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11215 if (kvm_run->immediate_exit) { in kvm_arch_vcpu_ioctl_run()
11216 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11228 if (kvm_run->kvm_valid_regs) in kvm_arch_vcpu_ioctl_run()
11240 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11248 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11249 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11251 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11252 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11253 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11254 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11255 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11256 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11257 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11258 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11260 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11261 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11262 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11263 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11264 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11265 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11266 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11267 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11270 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11271 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11284 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11285 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11287 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11288 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11289 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11290 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11291 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11292 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11293 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11294 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11296 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11297 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11298 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11299 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11300 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11301 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11302 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11303 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11306 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11307 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11309 vcpu->arch.exception.pending = false; in __set_regs()
11310 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11327 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11330 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11331 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11332 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11333 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11334 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11335 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11337 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11338 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11341 sregs->idt.limit = dt.size; in __get_sregs_common()
11342 sregs->idt.base = dt.address; in __get_sregs_common()
11344 sregs->gdt.limit = dt.size; in __get_sregs_common()
11345 sregs->gdt.base = dt.address; in __get_sregs_common()
11347 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11348 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11351 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11352 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11353 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11354 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11355 sregs->apic_base = kvm_get_apic_base(vcpu); in __get_sregs_common()
11362 if (vcpu->arch.guest_state_protected) in __get_sregs()
11365 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11366 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11367 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11376 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11381 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11382 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11411 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11412 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11413 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11414 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11416 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11430 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11434 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11457 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11458 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11459 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11462 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11463 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11464 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11466 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11478 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11486 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11487 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11488 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11492 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11493 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11500 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11503 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11506 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11508 if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11512 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11513 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11515 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11519 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11520 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11531 return -EINVAL; in __set_sregs_common()
11533 apic_base_msr.data = sregs->apic_base; in __set_sregs_common()
11536 return -EINVAL; in __set_sregs_common()
11538 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11541 dt.size = sregs->idt.limit; in __set_sregs_common()
11542 dt.address = sregs->idt.base; in __set_sregs_common()
11544 dt.size = sregs->gdt.limit; in __set_sregs_common()
11545 dt.address = sregs->gdt.base; in __set_sregs_common()
11548 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11549 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11550 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11552 static_call_cond(kvm_x86_post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11554 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11556 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11557 static_call(kvm_x86_set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11559 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11560 static_call(kvm_x86_set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11561 vcpu->arch.cr0 = sregs->cr0; in __set_sregs_common()
11563 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11564 static_call(kvm_x86_set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11567 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11572 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11575 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11576 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11577 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11578 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11579 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11580 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11582 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11583 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11589 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11591 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11610 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11623 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
11624 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
11625 !(sregs2->efer & EFER_LMA); in __set_sregs2()
11628 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
11629 return -EINVAL; in __set_sregs2()
11631 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
11632 return -EINVAL; in __set_sregs2()
11641 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
11645 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
11672 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11675 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11681 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
11690 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
11691 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
11695 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
11696 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
11699 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
11711 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
11712 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
11713 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
11715 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
11717 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11718 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11721 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
11725 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
11726 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
11736 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
11751 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
11757 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
11759 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
11760 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
11761 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
11762 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
11763 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
11773 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
11778 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
11779 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
11780 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
11781 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
11782 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
11783 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
11784 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
11785 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
11786 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
11796 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
11801 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
11803 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
11804 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
11805 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
11806 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
11807 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
11808 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
11809 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
11810 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
11820 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
11821 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
11823 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
11824 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
11826 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
11828 vcpu, &vcpu->run->s.regs.events); in store_regs()
11833 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
11834 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
11835 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
11838 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
11839 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
11842 return -EINVAL; in sync_regs()
11844 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
11847 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
11848 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
11851 return -EINVAL; in sync_regs()
11853 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
11861 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
11865 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
11866 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
11868 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
11869 return -EINVAL; in kvm_arch_vcpu_precreate()
11879 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
11880 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
11881 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
11883 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm, vcpu, KVM_HOST_USES_PFN); in kvm_arch_vcpu_create()
11885 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
11886 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
11888 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
11894 if (irqchip_in_kernel(vcpu->kvm)) { in kvm_arch_vcpu_create()
11905 * Ignore the current per-VM APICv state so that vCPU creation in kvm_arch_vcpu_create()
11907 * will ensure the vCPU gets the correct state before VM-Entry. in kvm_arch_vcpu_create()
11910 vcpu->arch.apic->apicv_active = true; in kvm_arch_vcpu_create()
11916 r = -ENOMEM; in kvm_arch_vcpu_create()
11921 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
11923 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
11925 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
11927 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
11929 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
11931 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
11938 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
11943 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); in kvm_arch_vcpu_create()
11944 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); in kvm_arch_vcpu_create()
11946 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_arch_vcpu_create()
11950 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
11953 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
11954 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
11957 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
11964 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
11965 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
11969 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
11976 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
11978 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
11980 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
11982 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
11983 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
11984 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
11994 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
11996 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12003 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12005 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12007 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12008 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12020 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12021 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12022 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12027 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12028 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12030 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12032 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12033 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12034 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12046 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12056 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12067 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12069 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12070 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12071 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12072 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12073 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12077 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12079 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12080 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12083 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12086 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12087 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12088 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12094 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12096 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12097 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12115 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12117 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12118 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12126 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12137 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12144 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12166 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12226 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_hardware_enable()
12228 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_hardware_enable()
12230 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_hardware_enable()
12231 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_hardware_enable()
12261 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_hardware_enable()
12275 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_hardware_enable()
12277 kvm->arch.backwards_tsc_observed = true; in kvm_arch_hardware_enable()
12279 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_hardware_enable()
12280 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_hardware_enable()
12290 kvm->arch.last_tsc_nsec = 0; in kvm_arch_hardware_enable()
12291 kvm->arch.last_tsc_write = 0; in kvm_arch_hardware_enable()
12306 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12311 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12321 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_sched_in()
12322 if (pmu->version && unlikely(pmu->event_count)) { in kvm_arch_sched_in()
12323 pmu->need_cleanup = true; in kvm_arch_sched_in()
12331 kfree(to_kvm_hv(kvm)->hv_pa_pg); in kvm_arch_free_vm()
12342 return -EINVAL; in kvm_arch_init_vm()
12354 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12355 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); in kvm_arch_init_vm()
12356 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12359 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12360 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12362 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12364 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12365 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12366 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12367 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12369 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12371 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12373 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12374 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12375 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12378 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12379 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12382 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12383 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12423 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12424 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12441 * -errno: on error
12446 * GPA->HVA translation will not change. However, the HVA is a user
12458 /* Called with kvm->slots_lock held. */ in __x86_set_memory_region()
12460 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12464 if (slot && slot->npages) in __x86_set_memory_region()
12465 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12476 if (!slot || !slot->npages) in __x86_set_memory_region()
12479 old_npages = slot->npages; in __x86_set_memory_region()
12480 hva = slot->userspace_addr; in __x86_set_memory_region()
12510 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12516 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12522 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12526 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12530 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12531 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12543 kvfree(slot->arch.rmap[i]); in memslot_rmap_free()
12544 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12555 kvfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12556 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12564 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12571 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12574 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12575 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12577 return -ENOMEM; in memslot_rmap_alloc()
12587 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12595 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12615 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12617 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12619 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12620 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12621 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12626 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
12643 kvfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
12644 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
12646 return -ENOMEM; in kvm_alloc_memslot_metadata()
12655 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
12660 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
12675 return -EINVAL; in kvm_arch_prepare_memory_region()
12678 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
12679 return -EINVAL; in kvm_arch_prepare_memory_region()
12685 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
12687 return -EIO; in kvm_arch_prepare_memory_region()
12700 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
12710 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
12711 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
12731 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
12740 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
12758 * which can be collapsed into a single large-page spte. Later in kvm_mmu_slot_apply_flags()
12759 * page faults will create the large-page sptes. in kvm_mmu_slot_apply_flags()
12764 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
12789 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
12796 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
12805 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
12806 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
12807 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
12808 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
12810 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
12813 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
12814 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
12817 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
12820 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
12834 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
12838 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
12858 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
12865 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
12872 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
12878 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
12895 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
12896 kvm_x86_ops.nested_ops->has_events(vcpu, false)) in kvm_vcpu_has_events()
12921 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
12936 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
12939 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_in_kernel()
12960 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
12981 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
12989 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
12990 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13011 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13018 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13021 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13030 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13031 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13039 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13048 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13052 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13055 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13057 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13064 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13073 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13081 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13090 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13103 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13112 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13130 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13145 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13146 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13154 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13177 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13180 if (work->wakeup_all) in kvm_arch_async_page_present()
13181 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13183 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13184 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13186 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13188 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13189 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13193 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13194 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13200 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13214 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13221 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13227 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13233 atomic_inc(&kvm->arch.noncoherent_dma_count); in kvm_arch_register_noncoherent_dma()
13239 atomic_dec(&kvm->arch.noncoherent_dma_count); in kvm_arch_unregister_noncoherent_dma()
13245 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13261 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13262 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13263 ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13264 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13267 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13279 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13280 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13284 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13288 ret = static_call(kvm_x86_pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13291 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13293 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13305 if (old->type != KVM_IRQ_ROUTING_MSI || in kvm_arch_irqfd_route_changed()
13306 new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13309 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13319 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13352 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13358 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13360 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13371 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13384 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13385 return -EIO; in kvm_handle_memory_failure()
13395 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13447 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13466 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13470 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13473 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13474 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13475 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13476 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13478 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13481 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13484 frag->data += len; in complete_sev_es_emulated_mmio()
13485 frag->gpa += len; in complete_sev_es_emulated_mmio()
13486 frag->len -= len; in complete_sev_es_emulated_mmio()
13489 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13490 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13498 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13499 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13500 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13501 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13502 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13503 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13505 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13517 return -EINVAL; in kvm_sev_es_mmio_write()
13523 bytes -= handled; in kvm_sev_es_mmio_write()
13528 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13529 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13530 frag->len = bytes; in kvm_sev_es_mmio_write()
13531 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13532 frag->data = data; in kvm_sev_es_mmio_write()
13534 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13535 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13537 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13538 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13539 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13540 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13541 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13543 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13556 return -EINVAL; in kvm_sev_es_mmio_read()
13562 bytes -= handled; in kvm_sev_es_mmio_read()
13567 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13568 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13569 frag->len = bytes; in kvm_sev_es_mmio_read()
13570 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13571 frag->data = data; in kvm_sev_es_mmio_read()
13573 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13574 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13576 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13577 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13578 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13579 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13581 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13589 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13590 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13598 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13599 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13601 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13602 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13612 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13613 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13621 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13625 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13634 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13635 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13636 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13638 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13640 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13650 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13651 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13656 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13660 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13668 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13669 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()