Lines Matching +full:dbg +full:- +full:halt
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>
109 ((struct kvm_vcpu *)(ctxt)->vcpu)
112 * - enable syscall per default because its emulated by KVM
113 * - enable LME and LMA per default on 64 bit KVM
144 *(((struct kvm_x86_ops *)0)->func));
147 #include <asm/kvm-x86-ops.h>
164 /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
176 * Flags to manipulate forced emulation behavior (any non-zero value will
183 int __read_mostly pi_inject_timer = -1;
443 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
456 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
548 size - useroffset, NULL); in kvm_alloc_emulator_cache()
557 vcpu->arch.apf.gfns[i] = ~0; in kvm_async_pf_hash_reset()
573 if (msrs->registered) { in kvm_on_user_return()
574 msrs->registered = false; in kvm_on_user_return()
579 values = &msrs->values[slot]; in kvm_on_user_return()
580 if (values->host != values->curr) { in kvm_on_user_return()
581 wrmsrl(kvm_uret_msrs_list[slot], values->host); in kvm_on_user_return()
582 values->curr = values->host; in kvm_on_user_return()
607 return -1; in kvm_add_user_return_msr()
622 return -1; in kvm_find_user_return_msr()
634 msrs->values[i].host = value; in kvm_user_return_msr_cpu_online()
635 msrs->values[i].curr = value; in kvm_user_return_msr_cpu_online()
644 value = (value & mask) | (msrs->values[slot].host & ~mask); in kvm_set_user_return_msr()
645 if (value == msrs->values[slot].curr) in kvm_set_user_return_msr()
651 msrs->values[slot].curr = value; in kvm_set_user_return_msr()
652 if (!msrs->registered) { in kvm_set_user_return_msr()
653 msrs->urn.on_user_return = kvm_on_user_return; in kvm_set_user_return_msr()
654 user_return_notifier_register(&msrs->urn); in kvm_set_user_return_msr()
655 msrs->registered = true; in kvm_set_user_return_msr()
665 if (msrs->registered) in drop_user_return_notifiers()
666 kvm_on_user_return(&msrs->urn); in drop_user_return_notifiers()
720 * #DBs can be trap-like or fault-like, the caller must check other CPU in exception_type()
739 if (!ex->has_payload) in kvm_deliver_exception_payload()
742 switch (ex->vector) { in kvm_deliver_exception_payload()
745 * "Certain debug exceptions may clear bit 0-3. The in kvm_deliver_exception_payload()
749 vcpu->arch.dr6 &= ~DR_TRAP_BITS; in kvm_deliver_exception_payload()
758 * Active low bits should be cleared if 1-setting in payload. in kvm_deliver_exception_payload()
759 * Active high bits should be set if 1-setting in payload. in kvm_deliver_exception_payload()
766 vcpu->arch.dr6 |= DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
767 vcpu->arch.dr6 |= ex->payload; in kvm_deliver_exception_payload()
768 vcpu->arch.dr6 ^= ex->payload & DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
776 vcpu->arch.dr6 &= ~BIT(12); in kvm_deliver_exception_payload()
779 vcpu->arch.cr2 = ex->payload; in kvm_deliver_exception_payload()
783 ex->has_payload = false; in kvm_deliver_exception_payload()
784 ex->payload = 0; in kvm_deliver_exception_payload()
792 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; in kvm_queue_exception_vmexit()
794 ex->vector = vector; in kvm_queue_exception_vmexit()
795 ex->injected = false; in kvm_queue_exception_vmexit()
796 ex->pending = true; in kvm_queue_exception_vmexit()
797 ex->has_error_code = has_error_code; in kvm_queue_exception_vmexit()
798 ex->error_code = error_code; in kvm_queue_exception_vmexit()
799 ex->has_payload = has_payload; in kvm_queue_exception_vmexit()
800 ex->payload = payload; in kvm_queue_exception_vmexit()
814 * morph it to a VM-Exit if L1 wants to intercept the exception. A in kvm_multiple_exception()
816 * when it was original queued, and re-checking is incorrect if _L1_ in kvm_multiple_exception()
820 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, nr, error_code)) { in kvm_multiple_exception()
826 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { in kvm_multiple_exception()
830 * On VM-Entry, an exception can be pending if and only in kvm_multiple_exception()
837 vcpu->arch.exception.injected = true; in kvm_multiple_exception()
847 vcpu->arch.exception.pending = true; in kvm_multiple_exception()
848 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
850 vcpu->arch.exception.has_error_code = has_error; in kvm_multiple_exception()
851 vcpu->arch.exception.vector = nr; in kvm_multiple_exception()
852 vcpu->arch.exception.error_code = error_code; in kvm_multiple_exception()
853 vcpu->arch.exception.has_payload = has_payload; in kvm_multiple_exception()
854 vcpu->arch.exception.payload = payload; in kvm_multiple_exception()
857 &vcpu->arch.exception); in kvm_multiple_exception()
862 prev_nr = vcpu->arch.exception.vector; in kvm_multiple_exception()
864 /* triple fault -> shutdown */ in kvm_multiple_exception()
876 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
877 vcpu->arch.exception.pending = false; in kvm_multiple_exception()
882 that instruction re-execution will regenerate lost in kvm_multiple_exception()
938 ++vcpu->stat.pf_guest; in kvm_inject_page_fault()
941 * Async #PF in L2 is always forwarded to L1 as a VM-Exit regardless of in kvm_inject_page_fault()
944 if (is_guest_mode(vcpu) && fault->async_page_fault) in kvm_inject_page_fault()
946 true, fault->error_code, in kvm_inject_page_fault()
947 true, fault->address); in kvm_inject_page_fault()
949 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, in kvm_inject_page_fault()
950 fault->address); in kvm_inject_page_fault()
957 WARN_ON_ONCE(fault->vector != PF_VECTOR); in kvm_inject_emulated_page_fault()
959 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : in kvm_inject_emulated_page_fault()
960 vcpu->arch.walk_mmu; in kvm_inject_emulated_page_fault()
966 if ((fault->error_code & PFERR_PRESENT_MASK) && in kvm_inject_emulated_page_fault()
967 !(fault->error_code & PFERR_RSVD_MASK)) in kvm_inject_emulated_page_fault()
968 kvm_mmu_invalidate_addr(vcpu, fault_mmu, fault->address, in kvm_inject_emulated_page_fault()
971 fault_mmu->inject_page_fault(vcpu, fault); in kvm_inject_emulated_page_fault()
977 atomic_inc(&vcpu->arch.nmi_queued); in kvm_inject_nmi()
1017 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); in pdptr_rsvd_bits()
1025 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in load_pdptrs()
1030 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; in load_pdptrs()
1058 if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) in load_pdptrs()
1059 kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); in load_pdptrs()
1061 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); in load_pdptrs()
1064 vcpu->arch.pdptrs_from_userspace = false; in load_pdptrs()
1089 * CR0.WP is incorporated into the MMU role, but only for non-nested, in kvm_post_set_cr0()
1136 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && in kvm_set_cr0()
1147 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && in kvm_set_cr0()
1172 if (vcpu->arch.guest_state_protected) in kvm_load_guest_xsave_state()
1177 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_guest_xsave_state()
1178 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); in kvm_load_guest_xsave_state()
1181 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_guest_xsave_state()
1182 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); in kvm_load_guest_xsave_state()
1186 vcpu->arch.pkru != vcpu->arch.host_pkru && in kvm_load_guest_xsave_state()
1187 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_guest_xsave_state()
1189 wrpkru(vcpu->arch.pkru); in kvm_load_guest_xsave_state()
1195 if (vcpu->arch.guest_state_protected) in kvm_load_host_xsave_state()
1199 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_host_xsave_state()
1201 vcpu->arch.pkru = rdpkru(); in kvm_load_host_xsave_state()
1202 if (vcpu->arch.pkru != vcpu->arch.host_pkru) in kvm_load_host_xsave_state()
1203 wrpkru(vcpu->arch.host_pkru); in kvm_load_host_xsave_state()
1208 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_host_xsave_state()
1212 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_host_xsave_state()
1222 return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; in kvm_guest_supported_xfd()
1229 u64 old_xcr0 = vcpu->arch.xcr0; in __kvm_set_xcr()
1245 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; in __kvm_set_xcr()
1264 vcpu->arch.xcr0 = xcr0; in __kvm_set_xcr()
1296 * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB in kvm_post_set_cr4()
1310 * - CR4.PCIDE is changed from 1 to 0 in kvm_post_set_cr4()
1311 * - CR4.PGE is toggled in kvm_post_set_cr4()
1322 * - CR4.SMEP is changed from 0 to 1 in kvm_post_set_cr4()
1323 * - CR4.PAE is toggled in kvm_post_set_cr4()
1365 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_invalidate_pcid()
1400 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) in kvm_invalidate_pcid()
1403 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); in kvm_invalidate_pcid()
1436 vcpu->arch.cr3 = cr3; in kvm_set_cr3()
1445 * and it's impossible to use a non-zero PCID when PCID is disabled, in kvm_set_cr3()
1462 vcpu->arch.cr8 = cr8; in kvm_set_cr8()
1472 return vcpu->arch.cr8; in kvm_get_cr8()
1480 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { in kvm_update_dr0123()
1482 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_update_dr0123()
1490 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) in kvm_update_dr7()
1491 dr7 = vcpu->arch.guest_debug_dr7; in kvm_update_dr7()
1493 dr7 = vcpu->arch.dr7; in kvm_update_dr7()
1495 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1497 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1515 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_set_dr()
1519 vcpu->arch.db[array_index_nospec(dr, size)] = val; in kvm_set_dr()
1520 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) in kvm_set_dr()
1521 vcpu->arch.eff_db[dr] = val; in kvm_set_dr()
1527 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); in kvm_set_dr()
1533 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; in kvm_set_dr()
1544 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_get_dr()
1548 return vcpu->arch.db[array_index_nospec(dr, size)]; in kvm_get_dr()
1551 return vcpu->arch.dr6; in kvm_get_dr()
1554 return vcpu->arch.dr7; in kvm_get_dr()
1578 * 10 - MISC_PACKAGE_CTRLS
1579 * 11 - ENERGY_FILTERING_CTL
1580 * 12 - DOITM
1581 * 18 - FB_CLEAR_CTRL
1582 * 21 - XAPIC_DISABLE_STATUS
1583 * 23 - OVERCLOCKING_STATUS
1714 u64 old_efer = vcpu->arch.efer; in set_efer()
1715 u64 efer = msr_info->data; in set_efer()
1721 if (!msr_info->host_initiated) { in set_efer()
1726 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1731 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1759 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1768 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1770 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1776 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1777 ranges = msr_filter->ranges; in kvm_msr_allowed()
1779 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1786 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1792 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1801 * Returns 0 on success, non-0 otherwise.
1822 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1823 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1824 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1826 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1828 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1830 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1849 * clear the bits. This ensures cross-vendor migration will in __kvm_set_msr()
1882 * Returns 0 on success, non-0 otherwise.
1949 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1950 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1951 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
1957 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
1968 return kvm_x86_call(complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
1997 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2000 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2001 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2002 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2003 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2004 vcpu->run->msr.index = index; in kvm_msr_user_space()
2005 vcpu->run->msr.data = data; in kvm_msr_user_space()
2006 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2022 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2023 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2083 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) && in kvm_emulate_monitor_mwait()
2106 return READ_ONCE(vcpu->mode) == EXITING_GUEST_MODE || in kvm_vcpu_exit_request()
2112 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2119 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2126 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2200 return -EINVAL; in do_set_msr()
2232 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2235 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2236 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2237 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2238 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2239 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2240 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2241 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2243 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2244 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2245 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2246 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2247 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2248 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2249 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2251 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2253 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2255 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2315 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2317 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2318 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2321 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2324 vcpu->arch.time = system_time; in kvm_write_system_time()
2329 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2332 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2355 shift--; in kvm_get_time_scale()
2400 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2401 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2405 return -1; in set_tsc_khz()
2409 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2414 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2416 return -1; in set_tsc_khz()
2432 return -1; in kvm_set_tsc_khz()
2437 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2438 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2439 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2447 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2459 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2460 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2461 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2462 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2476 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2484 bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2485 atomic_read(&vcpu->kvm->online_vcpus)) && in kvm_track_tsc_matching()
2486 gtod_is_based_on_tsc(gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2494 if ((ka->use_master_clock && new_generation) || in kvm_track_tsc_matching()
2495 (ka->use_master_clock != use_master_clock)) in kvm_track_tsc_matching()
2498 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, in kvm_track_tsc_matching()
2499 atomic_read(&vcpu->kvm->online_vcpus), in kvm_track_tsc_matching()
2500 ka->use_master_clock, gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2507 * The most significant 64-N bits (mult) of ratio represent the
2510 * point number (mult + frac * 2^(-N)).
2533 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); in kvm_compute_l1_tsc_offset()
2535 return target_tsc - tsc; in kvm_compute_l1_tsc_offset()
2540 return vcpu->arch.l1_tsc_offset + in kvm_read_l1_tsc()
2541 kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); in kvm_read_l1_tsc()
2572 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in kvm_vcpu_write_tsc_offset()
2573 vcpu->arch.l1_tsc_offset, in kvm_vcpu_write_tsc_offset()
2576 vcpu->arch.l1_tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2584 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( in kvm_vcpu_write_tsc_offset()
2589 vcpu->arch.tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2596 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2600 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( in kvm_vcpu_write_tsc_multiplier()
2604 vcpu->arch.tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2614 * TSC is marked unstable when we're running on Hyper-V, in kvm_check_tsc_unstable()
2631 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2633 lockdep_assert_held(&kvm->arch.tsc_write_lock); in __kvm_synchronize_tsc()
2639 kvm->arch.last_tsc_nsec = ns; in __kvm_synchronize_tsc()
2640 kvm->arch.last_tsc_write = tsc; in __kvm_synchronize_tsc()
2641 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; in __kvm_synchronize_tsc()
2642 kvm->arch.last_tsc_offset = offset; in __kvm_synchronize_tsc()
2644 vcpu->arch.last_guest_tsc = tsc; in __kvm_synchronize_tsc()
2656 * These values are tracked in kvm->arch.cur_xxx variables. in __kvm_synchronize_tsc()
2658 kvm->arch.cur_tsc_generation++; in __kvm_synchronize_tsc()
2659 kvm->arch.cur_tsc_nsec = ns; in __kvm_synchronize_tsc()
2660 kvm->arch.cur_tsc_write = tsc; in __kvm_synchronize_tsc()
2661 kvm->arch.cur_tsc_offset = offset; in __kvm_synchronize_tsc()
2662 kvm->arch.nr_vcpus_matched_tsc = 0; in __kvm_synchronize_tsc()
2663 } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { in __kvm_synchronize_tsc()
2664 kvm->arch.nr_vcpus_matched_tsc++; in __kvm_synchronize_tsc()
2668 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; in __kvm_synchronize_tsc()
2669 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; in __kvm_synchronize_tsc()
2670 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; in __kvm_synchronize_tsc()
2678 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2684 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2687 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2689 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2696 } else if (kvm->arch.user_set_tsc) { in kvm_synchronize_tsc()
2697 u64 tsc_exp = kvm->arch.last_tsc_write + in kvm_synchronize_tsc()
2699 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; in kvm_synchronize_tsc()
2701 * Here lies UAPI baggage: when a user-initiated TSC write has in kvm_synchronize_tsc()
2712 * come from the kernel's default vCPU creation. Make the 1-second in kvm_synchronize_tsc()
2721 kvm->arch.user_set_tsc = true; in kvm_synchronize_tsc()
2730 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2732 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2742 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2748 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2754 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2757 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2789 switch (clock->vclock_mode) { in vgettsc()
2795 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2796 clock->mask; in vgettsc()
2805 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2806 clock->mask; in vgettsc()
2815 return v * clock->mult; in vgettsc()
2820 * frequency of CLOCK_MONOTONIC_RAW (hence adding gtos->offs_boot).
2830 seq = read_seqcount_begin(>od->seq); in do_kvmclock_base()
2831 ns = gtod->raw_clock.base_cycles; in do_kvmclock_base()
2832 ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); in do_kvmclock_base()
2833 ns >>= gtod->raw_clock.shift; in do_kvmclock_base()
2834 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_kvmclock_base()
2835 } while (unlikely(read_seqcount_retry(>od->seq, seq))); in do_kvmclock_base()
2853 seq = read_seqcount_begin(>od->seq); in do_monotonic()
2854 ns = gtod->clock.base_cycles; in do_monotonic()
2855 ns += vgettsc(>od->clock, tsc_timestamp, &mode); in do_monotonic()
2856 ns >>= gtod->clock.shift; in do_monotonic()
2857 ns += ktime_to_ns(gtod->clock.offset); in do_monotonic()
2858 } while (unlikely(read_seqcount_retry(>od->seq, seq))); in do_monotonic()
2872 seq = read_seqcount_begin(>od->seq); in do_realtime()
2873 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2874 ns = gtod->clock.base_cycles; in do_realtime()
2875 ns += vgettsc(>od->clock, tsc_timestamp, &mode); in do_realtime()
2876 ns >>= gtod->clock.shift; in do_realtime()
2877 } while (unlikely(read_seqcount_retry(>od->seq, seq))); in do_realtime()
2879 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2880 ts->tv_nsec = ns; in do_realtime()
2948 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2949 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2950 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2954 * - ret0 < ret1
2955 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2957 * - 0 < N - M => M < N
2976 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
2980 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
2981 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
2982 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
2989 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
2990 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
2992 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
2993 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
2994 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
2996 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
3000 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
3012 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
3013 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3026 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3030 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3031 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3050 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3064 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3067 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3073 data->flags = 0; in __get_kvmclock()
3074 if (ka->use_master_clock && in __get_kvmclock()
3079 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3080 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3081 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3084 data->host_tsc = rdtsc(); in __get_kvmclock()
3086 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3087 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3088 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3092 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3094 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3102 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3106 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3108 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3124 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_setup_guest_pvclock()
3128 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3130 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3135 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3138 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3147 guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3151 vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3153 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_setup_guest_pvclock()
3154 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; in kvm_setup_guest_pvclock()
3155 vcpu->pvclock_set_guest_stopped_request = false; in kvm_setup_guest_pvclock()
3158 memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); in kvm_setup_guest_pvclock()
3161 guest_hv_clock->flags &= ~PVCLOCK_TSC_STABLE_BIT; in kvm_setup_guest_pvclock()
3165 guest_hv_clock->version = ++vcpu->hv_clock.version; in kvm_setup_guest_pvclock()
3168 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3170 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); in kvm_setup_guest_pvclock()
3177 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3178 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3191 ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE; in kvm_guest_time_update()
3202 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3203 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3205 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3206 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3208 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3235 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3238 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3249 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3251 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3253 &vcpu->hv_clock.tsc_shift, in kvm_guest_time_update()
3254 &vcpu->hv_clock.tsc_to_system_mul); in kvm_guest_time_update()
3255 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3259 vcpu->hv_clock.tsc_timestamp = tsc_timestamp; in kvm_guest_time_update()
3260 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3261 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3268 vcpu->hv_clock.flags = pvclock_flags; in kvm_guest_time_update()
3270 if (vcpu->pv_time.active) in kvm_guest_time_update()
3271 kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0, false); in kvm_guest_time_update()
3273 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3274 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3277 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3278 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0, in kvm_guest_time_update()
3281 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); in kvm_guest_time_update()
3308 struct kvm_arch *ka = &kvm->arch; in kvm_get_wall_clock_epoch()
3314 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_get_wall_clock_epoch()
3317 if (!ka->use_master_clock) in kvm_get_wall_clock_epoch()
3339 hv_clock.tsc_timestamp = ka->master_cycle_now; in kvm_get_wall_clock_epoch()
3340 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in kvm_get_wall_clock_epoch()
3342 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_get_wall_clock_epoch()
3348 * since 1970-01-01. in kvm_get_wall_clock_epoch()
3354 return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - in kvm_get_wall_clock_epoch()
3358 return ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_get_wall_clock_epoch()
3363 * vcpu->cpu migration, should not allow system_timestamp from
3369 * We need to rate-limit these requests though, as they can
3372 * by the delay we use to rate-limit the updates.
3394 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3397 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3410 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3411 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3432 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3439 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3441 u32 msr = msr_info->index; in set_msr_mce()
3442 u64 data = msr_info->data; in set_msr_mce()
3447 vcpu->arch.mcg_status = data; in set_msr_mce()
3451 (data || !msr_info->host_initiated)) in set_msr_mce()
3455 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3457 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3458 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3462 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3467 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3468 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3469 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3471 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3472 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3484 * single-bit ECC data errors. in set_msr_mce()
3492 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3495 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3499 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3500 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3501 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3513 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3535 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3543 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3547 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3548 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3557 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3564 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3566 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3573 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3574 vcpu->arch.time = 0; in kvmclock_reset()
3579 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3588 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3604 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3613 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3621 * prior before nested VM-Enter/VM-Exit.
3635 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3638 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3642 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3647 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3650 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3653 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3655 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3656 gpa != ghc->gpa || in record_steal_time()
3657 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3659 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3661 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3662 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3666 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3673 int err = -EFAULT; in record_steal_time()
3684 "+m" (st->preempted)); in record_steal_time()
3690 vcpu->arch.st.preempted = 0; in record_steal_time()
3692 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3703 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3704 vcpu->arch.st.preempted = 0; in record_steal_time()
3707 unsafe_get_user(version, &st->version, out); in record_steal_time()
3712 unsafe_put_user(version, &st->version, out); in record_steal_time()
3716 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3717 steal += current->sched_info.run_delay - in record_steal_time()
3718 vcpu->arch.st.last_steal; in record_steal_time()
3719 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3720 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3723 unsafe_put_user(version, &st->version, out); in record_steal_time()
3728 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3733 u32 msr = msr_info->index; in kvm_set_msr_common()
3734 u64 data = msr_info->data; in kvm_set_msr_common()
3736 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) in kvm_set_msr_common()
3751 if (msr_info->host_initiated) in kvm_set_msr_common()
3752 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3755 if (!msr_info->host_initiated || in kvm_set_msr_common()
3758 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3761 if (!msr_info->host_initiated || in kvm_set_msr_common()
3773 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3776 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3782 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3810 if (!msr_info->host_initiated && in kvm_set_msr_common()
3837 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3849 vcpu->arch.pat = data; in kvm_set_msr_common()
3855 return kvm_apic_set_base(vcpu, data, msr_info->host_initiated); in kvm_set_msr_common()
3863 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3864 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3871 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3875 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3877 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3887 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3891 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3894 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3899 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3901 vcpu->arch.smbase = data; in kvm_set_msr_common()
3904 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3907 if (msr_info->host_initiated) { in kvm_set_msr_common()
3910 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3912 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3916 if (!msr_info->host_initiated && in kvm_set_msr_common()
3926 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3930 if (!msr_info->host_initiated) in kvm_set_msr_common()
3932 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3938 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3939 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3945 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3946 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3952 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
3958 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
3978 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
3992 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
4013 if (data & (-1ULL << 1)) in kvm_set_msr_common()
4016 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
4021 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4022 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4039 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
4057 msr_info->host_initiated); in kvm_set_msr_common()
4060 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
4068 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
4073 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
4076 if (!msr_info->host_initiated) in kvm_set_msr_common()
4078 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
4085 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
4089 if (!msr_info->host_initiated && in kvm_set_msr_common()
4096 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
4099 if (!msr_info->host_initiated && in kvm_set_msr_common()
4106 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
4122 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4132 data = vcpu->arch.mcg_cap; in get_msr_mce()
4137 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4140 data = vcpu->arch.mcg_status; in get_msr_mce()
4142 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4143 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4149 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4150 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4151 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4153 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4154 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4158 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4159 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4160 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4171 switch (msr_info->index) { in kvm_get_msr_common()
4194 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4201 msr_info->data = 0; in kvm_get_msr_common()
4207 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4209 msr_info->data = 0; in kvm_get_msr_common()
4212 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4217 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4222 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4225 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4234 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4239 if (msr_info->host_initiated) { in kvm_get_msr_common()
4240 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4241 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4243 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4244 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4247 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4251 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4256 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4258 msr_info->data = 3; in kvm_get_msr_common()
4272 msr_info->data = 1 << 24; in kvm_get_msr_common()
4275 msr_info->data = vcpu->arch.apic_base; in kvm_get_msr_common()
4278 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4280 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4283 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4286 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4289 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4291 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4294 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4298 msr_info->data = 1000ULL; in kvm_get_msr_common()
4300 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4303 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4309 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4315 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4321 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4327 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4333 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4339 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4345 msr_info->data = 0; in kvm_get_msr_common()
4351 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4357 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4363 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4370 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4371 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4372 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4373 msr_info->host_initiated); in kvm_get_msr_common()
4375 if (!msr_info->host_initiated && in kvm_get_msr_common()
4378 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4382 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4390 msr_info->data = 0x20000000; in kvm_get_msr_common()
4404 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4405 msr_info->host_initiated); in kvm_get_msr_common()
4418 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4423 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4428 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4431 if (!msr_info->host_initiated && in kvm_get_msr_common()
4432 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4434 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4437 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4440 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4444 if (!msr_info->host_initiated && in kvm_get_msr_common()
4448 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4451 if (!msr_info->host_initiated && in kvm_get_msr_common()
4455 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4459 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4480 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4502 r = -EFAULT; in msr_io()
4506 r = -E2BIG; in msr_io()
4511 entries = memdup_user(user_msrs->entries, size); in msr_io()
4519 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4520 r = -EFAULT; in msr_io()
4555 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4559 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4563 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4732 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4733 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4740 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4785 if (attr->group) { in __kvm_x86_dev_get_attr()
4787 return kvm_x86_call(dev_get_attr)(attr->group, attr->attr, val); in __kvm_x86_dev_get_attr()
4788 return -ENXIO; in __kvm_x86_dev_get_attr()
4791 switch (attr->attr) { in __kvm_x86_dev_get_attr()
4796 return -ENXIO; in __kvm_x86_dev_get_attr()
4802 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_x86_dev_get_attr()
4811 return -EFAULT; in kvm_x86_dev_get_attr()
4835 r = -EFAULT; in kvm_arch_dev_ioctl()
4842 r = -E2BIG; in kvm_arch_dev_ioctl()
4845 r = -EFAULT; in kvm_arch_dev_ioctl()
4846 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4849 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4861 r = -EFAULT; in kvm_arch_dev_ioctl()
4865 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4870 r = -EFAULT; in kvm_arch_dev_ioctl()
4877 r = -EFAULT; in kvm_arch_dev_ioctl()
4888 r = -EFAULT; in kvm_arch_dev_ioctl()
4895 r = -E2BIG; in kvm_arch_dev_ioctl()
4898 r = -EFAULT; in kvm_arch_dev_ioctl()
4899 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4915 r = -EFAULT; in kvm_arch_dev_ioctl()
4923 r = -EFAULT; in kvm_arch_dev_ioctl()
4930 r = -EINVAL; in kvm_arch_dev_ioctl()
4944 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4951 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_vcpu_load()
4953 if (vcpu->scheduled_out && pmu->version && pmu->event_count) { in kvm_arch_vcpu_load()
4954 pmu->need_cleanup = true; in kvm_arch_vcpu_load()
4961 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4962 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4963 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
4970 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
4973 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
4974 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
4975 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
4979 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
4980 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
4981 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
4987 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
4989 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
4997 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
4999 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
5001 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5003 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
5011 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
5015 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
5018 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
5022 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
5024 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
5025 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
5029 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
5030 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
5033 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
5037 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
5040 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
5042 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
5043 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
5044 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
5047 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
5048 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
5050 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
5051 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
5053 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
5060 if (vcpu->preempted) { in kvm_arch_vcpu_put()
5062 * Assume protected guests are in-kernel. Inefficient yielding in kvm_arch_vcpu_put()
5066 vcpu->arch.preempted_in_kernel = vcpu->arch.guest_state_protected || in kvm_arch_vcpu_put()
5073 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
5074 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
5078 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
5082 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
5129 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
5140 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
5141 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
5143 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
5144 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5150 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5151 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5153 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5154 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5156 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5157 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5159 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5174 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5175 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5176 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5186 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5192 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5195 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5198 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5200 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5213 * - none of the bits for Machine Check Exceptions are set
5214 * - both the VAL (valid) and UC (uncorrectable) bits are set
5215 * MCI_STATUS_PCC - Processor Context Corrupted
5216 * MCI_STATUS_S - Signaled as a Machine Check Exception
5217 * MCI_STATUS_AR - Software recoverable Action Required
5221 return !mce->mcg_status && in is_ucna()
5222 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5223 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5224 (mce->status & MCI_STATUS_UC); in is_ucna()
5229 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5231 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5232 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5233 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5234 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5237 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5241 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5249 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5251 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5253 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5254 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5256 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5265 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5266 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5272 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5274 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5275 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5281 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5282 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5283 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5284 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5285 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5290 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5291 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5292 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5293 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5314 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5315 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5318 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5319 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5320 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5321 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5323 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5328 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5333 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5334 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5345 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5346 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5347 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5353 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5354 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5356 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5357 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5358 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5359 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5360 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5362 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5363 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5364 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5365 events->interrupt.shadow = kvm_x86_call(get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5367 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5368 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5369 events->nmi.masked = kvm_x86_call(get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5371 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5374 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5375 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5376 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5377 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5379 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5381 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5384 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5385 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5386 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5387 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5388 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5395 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5401 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5403 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5404 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5405 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5406 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5407 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5409 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5411 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5412 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5415 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5416 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5417 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5420 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5421 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5422 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5423 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5429 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5430 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5433 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5435 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5437 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5439 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5440 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5441 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5442 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5443 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5444 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5445 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5447 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5448 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5449 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5450 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5452 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5454 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5455 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5456 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5457 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5458 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5461 kvm_x86_call(set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5463 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5465 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5467 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5469 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5471 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5474 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5476 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5477 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5478 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5480 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5484 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5485 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5486 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5490 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5491 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5493 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5497 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5498 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5499 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5500 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5516 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_debugregs()
5517 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_debugregs()
5518 return -EINVAL; in kvm_vcpu_ioctl_x86_get_debugregs()
5522 BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.db) != ARRAY_SIZE(dbgregs->db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5523 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_get_debugregs()
5524 dbgregs->db[i] = vcpu->arch.db[i]; in kvm_vcpu_ioctl_x86_get_debugregs()
5526 dbgregs->dr6 = vcpu->arch.dr6; in kvm_vcpu_ioctl_x86_get_debugregs()
5527 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5536 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_debugregs()
5537 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_debugregs()
5538 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5540 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5541 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5543 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5544 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5545 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5546 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5548 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_set_debugregs()
5549 vcpu->arch.db[i] = dbgregs->db[i]; in kvm_vcpu_ioctl_x86_set_debugregs()
5552 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5553 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5575 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5578 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5579 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_get_xsave2()
5581 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5582 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5589 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5590 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5596 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5597 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_set_xsave()
5599 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5600 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5602 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5608 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_xcrs()
5609 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_xcrs()
5610 return -EINVAL; in kvm_vcpu_ioctl_x86_get_xcrs()
5613 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5617 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5618 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5619 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5620 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5629 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_xcrs()
5630 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_xcrs()
5631 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5634 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5636 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5637 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5639 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5641 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5643 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5647 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5659 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5660 return -EINVAL; in kvm_set_guest_paused()
5661 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5671 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5676 r = -ENXIO; in kvm_arch_tsc_has_attr()
5685 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_get_attr()
5688 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5690 r = -EFAULT; in kvm_arch_tsc_get_attr()
5691 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5696 r = -ENXIO; in kvm_arch_tsc_get_attr()
5705 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_set_attr()
5706 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5709 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5715 r = -EFAULT; in kvm_arch_tsc_set_attr()
5719 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5721 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5722 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5723 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5725 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5728 kvm->arch.user_set_tsc = true; in kvm_arch_tsc_set_attr()
5730 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5736 r = -ENXIO; in kvm_arch_tsc_set_attr()
5750 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5753 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5773 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5774 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5776 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5779 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5780 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5784 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5785 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5786 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5794 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5795 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5796 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5798 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5801 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5807 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5812 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5816 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5819 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5826 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5842 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5847 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5853 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5860 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5875 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5893 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5896 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5903 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5907 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5914 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5918 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5921 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5928 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5930 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5934 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5936 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5942 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5948 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5958 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5961 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5964 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5966 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5972 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5981 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5992 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6001 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6017 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6027 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6036 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6037 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
6041 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6049 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6056 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6069 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6072 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6080 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6090 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6098 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6118 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6134 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
6144 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6154 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6155 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
6158 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
6159 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6160 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6163 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
6169 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6170 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6172 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6184 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6185 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6188 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6192 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6207 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6208 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6209 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6221 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6226 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6232 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6240 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6241 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6242 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6246 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6250 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6257 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6258 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6259 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6277 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6295 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6296 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6311 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6313 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6316 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6318 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6324 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6328 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6330 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6334 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6338 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6341 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6349 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6353 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6355 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6356 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6358 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6361 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6362 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6364 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6367 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6370 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6379 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6381 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6383 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6384 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6385 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6392 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6394 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6395 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6397 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6398 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6404 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6405 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6406 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6407 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6408 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6409 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6418 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6420 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6421 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6422 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6425 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6426 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6427 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6429 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6431 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6438 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6440 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6444 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6445 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6446 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6457 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6458 * VM-Exit. in kvm_arch_sync_dirty_log()
6474 return -ENXIO; in kvm_vm_ioctl_irq_line()
6476 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6477 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6487 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6488 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6490 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6492 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6493 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6497 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6501 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6502 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6503 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6505 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6508 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6512 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6513 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6517 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6521 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6522 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6525 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6526 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6527 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6528 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6533 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6534 if (cap->args[0] & ~kvm_get_allowed_disable_exits()) in kvm_vm_ioctl_enable_cap()
6537 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6538 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6541 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6546 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6549 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6550 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6551 if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) in kvm_vm_ioctl_enable_cap()
6552 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6553 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6554 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6555 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6556 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6559 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6562 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6566 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6570 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6574 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6575 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6577 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6581 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6582 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6585 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6586 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6590 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6591 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6598 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6605 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6607 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6612 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6616 r = kvm_x86_call(vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6619 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6623 r = kvm_x86_call(vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6626 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6627 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6630 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6634 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6635 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6637 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6641 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6642 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6645 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6646 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6647 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6650 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6653 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6654 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6657 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6658 if (kvm->arch.bsp_vcpu_id > cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6660 } else if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6662 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6663 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6666 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6669 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6670 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6674 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6676 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6677 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6678 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6679 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6682 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6685 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6699 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6703 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6706 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6707 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6708 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6711 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6714 u64 bus_cycle_ns = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6721 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6727 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6729 r = -ENXIO; in kvm_vm_ioctl_enable_cap()
6730 else if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6731 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6733 kvm->arch.apic_bus_cycle_ns = bus_cycle_ns; in kvm_vm_ioctl_enable_cap()
6734 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6738 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6752 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6763 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6764 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6775 if (!user_range->nmsrs) in kvm_add_msr_filter()
6778 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6779 return -EINVAL; in kvm_add_msr_filter()
6781 if (!user_range->flags) in kvm_add_msr_filter()
6782 return -EINVAL; in kvm_add_msr_filter()
6784 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6786 return -EINVAL; in kvm_add_msr_filter()
6788 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6792 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6793 .flags = user_range->flags, in kvm_add_msr_filter()
6794 .base = user_range->base, in kvm_add_msr_filter()
6795 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6799 msr_filter->count++; in kvm_add_msr_filter()
6812 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6813 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6815 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6816 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6818 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6820 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6824 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6826 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6827 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6834 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6835 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6836 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6837 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6838 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6867 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6868 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6879 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6887 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6888 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6889 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6890 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6910 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6912 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6918 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6922 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6945 return -EFAULT; in kvm_vm_ioctl_get_clock()
6952 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6957 return -EFAULT; in kvm_vm_ioctl_set_clock()
6964 return -EINVAL; in kvm_vm_ioctl_set_clock()
6984 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
6987 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
6988 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
6991 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
6998 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
7000 int r = -ENOTTY; in kvm_arch_vm_ioctl()
7002 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
7019 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7020 r = -EINVAL; in kvm_arch_vm_ioctl()
7021 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7023 r = -EFAULT; in kvm_arch_vm_ioctl()
7028 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7035 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7037 r = -EEXIST; in kvm_arch_vm_ioctl()
7041 r = -EINVAL; in kvm_arch_vm_ioctl()
7042 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7061 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
7063 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
7066 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7073 r = -EFAULT; in kvm_arch_vm_ioctl()
7078 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7079 r = -EEXIST; in kvm_arch_vm_ioctl()
7080 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7082 r = -ENOENT; in kvm_arch_vm_ioctl()
7085 r = -ENOMEM; in kvm_arch_vm_ioctl()
7086 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
7087 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7090 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7102 r = -ENXIO; in kvm_arch_vm_ioctl()
7108 r = -EFAULT; in kvm_arch_vm_ioctl()
7126 r = -ENXIO; in kvm_arch_vm_ioctl()
7135 r = -EFAULT; in kvm_arch_vm_ioctl()
7138 r = -ENXIO; in kvm_arch_vm_ioctl()
7139 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7144 r = -EFAULT; in kvm_arch_vm_ioctl()
7151 r = -EFAULT; in kvm_arch_vm_ioctl()
7154 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7155 r = -ENXIO; in kvm_arch_vm_ioctl()
7156 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7160 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7164 r = -ENXIO; in kvm_arch_vm_ioctl()
7165 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7170 r = -EFAULT; in kvm_arch_vm_ioctl()
7177 r = -EFAULT; in kvm_arch_vm_ioctl()
7180 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7181 r = -ENXIO; in kvm_arch_vm_ioctl()
7182 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7186 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7191 r = -EFAULT; in kvm_arch_vm_ioctl()
7194 r = -ENXIO; in kvm_arch_vm_ioctl()
7195 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7202 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7203 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7204 r = -EBUSY; in kvm_arch_vm_ioctl()
7206 (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) in kvm_arch_vm_ioctl()
7207 r = -EINVAL; in kvm_arch_vm_ioctl()
7209 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
7210 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7215 r = -EFAULT; in kvm_arch_vm_ioctl()
7224 r = -EFAULT; in kvm_arch_vm_ioctl()
7229 r = -EFAULT; in kvm_arch_vm_ioctl()
7235 r = -EFAULT; in kvm_arch_vm_ioctl()
7244 r = -EFAULT; in kvm_arch_vm_ioctl()
7260 r = -EINVAL; in kvm_arch_vm_ioctl()
7270 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7276 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7280 r = -ENOTTY; in kvm_arch_vm_ioctl()
7290 r = -EFAULT; in kvm_arch_vm_ioctl()
7294 r = -ENOTTY; in kvm_arch_vm_ioctl()
7304 r = -EFAULT; in kvm_arch_vm_ioctl()
7308 r = -ENOTTY; in kvm_arch_vm_ioctl()
7319 r = -EFAULT; in kvm_arch_vm_ioctl()
7334 return -EFAULT; in kvm_arch_vm_ioctl()
7340 r = -ENOTTY; in kvm_arch_vm_ioctl()
7400 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7405 MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7406 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7411 MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7412 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7417 MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1: in kvm_probe_msr_to_save()
7418 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7487 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7492 len -= n; in vcpu_mmio_write()
7507 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7514 len -= n; in vcpu_mmio_read()
7536 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7541 /* NPT walks are always user-walks */ in translate_nested_gpa()
7543 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7551 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7554 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7561 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7565 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7573 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7575 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7582 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7587 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7588 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7589 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7601 bytes -= toread; in kvm_read_guest_virt_helper()
7615 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7621 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7626 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7628 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7674 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7679 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7680 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7681 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7692 bytes -= towrite; in kvm_write_guest_virt_helper()
7720 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7780 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7790 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7791 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7792 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7793 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7798 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7801 return -1; in vcpu_mmio_gva_to_gpa()
7832 if (vcpu->mmio_read_completed) { in read_prepare()
7834 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7835 vcpu->mmio_read_completed = 0; in read_prepare()
7870 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7872 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7898 bool write = ops->write; in emulator_read_write_onepage()
7900 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7909 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7910 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7911 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7919 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7925 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7930 bytes -= handled; in emulator_read_write_onepage()
7933 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7934 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7935 frag->gpa = gpa; in emulator_read_write_onepage()
7936 frag->data = val; in emulator_read_write_onepage()
7937 frag->len = bytes; in emulator_read_write_onepage()
7951 if (ops->read_write_prepare && in emulator_read_write()
7952 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7955 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7958 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7961 now = -addr & ~PAGE_MASK; in emulator_read_write()
7968 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
7971 bytes -= now; in emulator_read_write()
7979 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
7982 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
7984 vcpu->mmio_needed = 1; in emulator_read_write()
7985 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
7987 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
7988 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
7989 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
7990 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
7992 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
8032 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
8046 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
8050 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
8108 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
8124 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
8133 vcpu->arch.pio.port = port; in emulator_pio_in_out()
8134 vcpu->arch.pio.in = in; in emulator_pio_in_out()
8135 vcpu->arch.pio.count = count; in emulator_pio_in_out()
8136 vcpu->arch.pio.size = size; in emulator_pio_in_out()
8139 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
8141 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
8143 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
8144 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
8145 vcpu->run->io.size = size; in emulator_pio_in_out()
8146 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
8147 vcpu->run->io.count = count; in emulator_pio_in_out()
8148 vcpu->run->io.port = port; in emulator_pio_in_out()
8164 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
8165 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
8166 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
8167 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
8168 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
8176 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
8224 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8225 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
8228 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8262 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8275 value = vcpu->arch.cr2; in emulator_get_cr()
8304 vcpu->arch.cr2 = val; in emulator_set_cr()
8317 res = -1; in emulator_set_cr()
8378 desc->type = var.type; in emulator_get_segment()
8379 desc->s = var.s; in emulator_get_segment()
8380 desc->dpl = var.dpl; in emulator_get_segment()
8381 desc->p = var.present; in emulator_get_segment()
8382 desc->avl = var.avl; in emulator_get_segment()
8383 desc->l = var.l; in emulator_get_segment()
8384 desc->d = var.db; in emulator_get_segment()
8385 desc->g = var.g; in emulator_get_segment()
8403 if (desc->g) in emulator_set_segment()
8405 var.type = desc->type; in emulator_set_segment()
8406 var.dpl = desc->dpl; in emulator_set_segment()
8407 var.db = desc->d; in emulator_set_segment()
8408 var.s = desc->s; in emulator_set_segment()
8409 var.l = desc->l; in emulator_set_segment()
8410 var.g = desc->g; in emulator_set_segment()
8411 var.avl = desc->avl; in emulator_set_segment()
8412 var.present = desc->p; in emulator_set_segment()
8485 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8493 &ctxt->exception); in emulator_intercept()
8568 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8570 if (!kvm->vm_bugged) in emulator_vm_bugged()
8620 .halt = emulator_halt,
8660 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8662 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8663 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8664 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8665 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8666 ctxt->exception.error_code); in inject_emulated_exception()
8668 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8681 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8682 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8683 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8690 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8695 ctxt->gpa_available = false; in init_emulate_ctxt()
8696 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8697 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8699 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8700 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8701 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8705 ctxt->interruptibility = 0; in init_emulate_ctxt()
8706 ctxt->have_exception = false; in init_emulate_ctxt()
8707 ctxt->exception.vector = -1; in init_emulate_ctxt()
8708 ctxt->perm_ok = false; in init_emulate_ctxt()
8711 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8716 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8721 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8722 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8723 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8729 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8730 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8731 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8739 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8752 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8753 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8765 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8768 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8769 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8771 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8773 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8774 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8775 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8776 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8779 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8780 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8783 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8788 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8790 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8791 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8810 struct kvm_run *run = vcpu->run; in kvm_prepare_event_vectoring_exit()
8817 run->internal.data[ndata++] = info2; in kvm_prepare_event_vectoring_exit()
8818 run->internal.data[ndata++] = reason; in kvm_prepare_event_vectoring_exit()
8819 run->internal.data[ndata++] = info1; in kvm_prepare_event_vectoring_exit()
8820 run->internal.data[ndata++] = gpa; in kvm_prepare_event_vectoring_exit()
8821 run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu; in kvm_prepare_event_vectoring_exit()
8823 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_prepare_event_vectoring_exit()
8824 run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; in kvm_prepare_event_vectoring_exit()
8825 run->internal.ndata = ndata; in kvm_prepare_event_vectoring_exit()
8831 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8833 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8841 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8870 * a SPTE and write-protect the gfn to resolve the !PRESENT fault, and in kvm_unprotect_and_retry_on_failure()
8879 * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the in kvm_unprotect_and_retry_on_failure()
8880 * guest to let the CPU re-execute the instruction in the hope that the in kvm_unprotect_and_retry_on_failure()
8915 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8917 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8918 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8919 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8920 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8921 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8991 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8992 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8993 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
8996 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
8997 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
9000 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
9001 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
9002 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
9003 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
9009 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
9013 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
9014 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
9028 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
9030 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9047 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9060 * (and wrong) when emulating on an intercepted fault-like exception[*], as
9070 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
9078 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
9088 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
9114 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
9121 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
9139 if (ctxt->have_exception && in x86_emulate_instruction()
9142 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
9145 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
9146 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
9164 * injecting single-step #DBs. in x86_emulate_instruction()
9167 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
9168 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
9170 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
9177 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9178 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
9179 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
9184 * If emulation was caused by a write-protection #PF on a non-page_table in x86_emulate_instruction()
9196 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
9197 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
9204 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
9207 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
9208 ctxt->gpa_available = true; in x86_emulate_instruction()
9209 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
9213 ctxt->exception.address = 0; in x86_emulate_instruction()
9229 if (ctxt->have_exception) { in x86_emulate_instruction()
9230 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9231 vcpu->mmio_needed = false; in x86_emulate_instruction()
9234 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9235 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9236 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9237 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9240 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9243 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9244 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9246 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9249 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9250 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9261 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9262 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9265 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9267 * of which are fault-like. in x86_emulate_instruction()
9269 if (!ctxt->have_exception || in x86_emulate_instruction()
9270 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9272 if (ctxt->is_branch) in x86_emulate_instruction()
9274 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9275 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9278 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9287 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9290 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9310 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9316 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9318 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9338 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9339 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9343 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9344 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9354 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9356 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9357 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9362 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9385 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9386 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9417 khz = freq->new; in tsc_khz_changed()
9438 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9506 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9509 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9515 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9538 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9540 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9543 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9571 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9572 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9630 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9643 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9653 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9656 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9674 return -EIO; in kvm_x86_check_processor_compatibility()
9693 return -EEXIST; in kvm_x86_vendor_init()
9703 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9708 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9721 return -EIO; in kvm_x86_vendor_init()
9729 return -ENOMEM; in kvm_x86_vendor_init()
9735 r = -ENOMEM; in kvm_x86_vendor_init()
9757 kvm_init_pmu_capability(ops->pmu_ops); in kvm_x86_vendor_init()
9762 r = ops->hardware_setup(); in kvm_x86_vendor_init()
9781 if (pi_inject_timer == -1) in kvm_x86_vendor_init()
9790 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in kvm_x86_vendor_init()
9870 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9876 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9877 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9880 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9889 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9891 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9900 * @apicid - apicid of vcpu to be kicked.
9920 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9926 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9954 set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason, true); in kvm_apicv_init()
9956 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9964 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9970 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9972 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9973 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9977 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9987 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
9995 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
10011 ++vcpu->stat.hypercalls; in ____kvm_emulate_hypercall()
10024 ret = -KVM_EPERM; in ____kvm_emulate_hypercall()
10028 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10038 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in ____kvm_emulate_hypercall()
10051 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in ____kvm_emulate_hypercall()
10063 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10064 if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) in ____kvm_emulate_hypercall()
10069 ret = -KVM_EINVAL; in ____kvm_emulate_hypercall()
10073 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in ____kvm_emulate_hypercall()
10074 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in ____kvm_emulate_hypercall()
10076 * In principle this should have been -KVM_ENOSYS, but userspace (QEMU <=9.2) in ____kvm_emulate_hypercall()
10077 * assumed that vcpu->run->hypercall.ret is never changed by KVM and thus that in ____kvm_emulate_hypercall()
10079 * vcpu->run->hypercall.ret, ensuring that it is zero to not break QEMU. in ____kvm_emulate_hypercall()
10081 vcpu->run->hypercall.ret = 0; in ____kvm_emulate_hypercall()
10082 vcpu->run->hypercall.args[0] = gpa; in ____kvm_emulate_hypercall()
10083 vcpu->run->hypercall.args[1] = npages; in ____kvm_emulate_hypercall()
10084 vcpu->run->hypercall.args[2] = attrs; in ____kvm_emulate_hypercall()
10085 vcpu->run->hypercall.flags = 0; in ____kvm_emulate_hypercall()
10087 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in ____kvm_emulate_hypercall()
10089 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in ____kvm_emulate_hypercall()
10090 vcpu->arch.complete_userspace_io = complete_hypercall; in ____kvm_emulate_hypercall()
10094 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10099 vcpu->run->hypercall.ret = ret; in ____kvm_emulate_hypercall()
10106 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
10129 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
10130 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
10131 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
10132 ctxt->have_exception = true; in emulator_fix_hypercall()
10139 &ctxt->exception); in emulator_fix_hypercall()
10144 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
10145 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
10148 /* Called within kvm->srcu read side. */
10151 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
10153 kvm_run->if_flag = kvm_x86_call(get_if_flag)(vcpu); in post_kvm_run_save()
10154 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
10155 kvm_run->apic_base = vcpu->arch.apic_base; in post_kvm_run_save()
10157 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
10158 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
10162 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
10164 kvm_run->flags |= KVM_RUN_X86_GUEST_MODE; in post_kvm_run_save()
10177 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10180 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10183 max_irr = -1; in update_cr8_intercept()
10185 if (max_irr != -1) in update_cr8_intercept()
10197 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10201 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10210 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10213 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10215 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10216 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10217 vcpu->arch.exception.error_code, in kvm_inject_exception()
10218 vcpu->arch.exception.injected); in kvm_inject_exception()
10228 * injected as part of a previous VM-Enter, but weren't successfully delivered
10229 * and need to be re-injected.
10234 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10235 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10240 * instruction boundaries for asynchronous events. However, because VM-Exits
10246 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10269 * Process nested events first, as nested VM-Exit supersedes event in kvm_check_and_inject_events()
10270 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10271 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10279 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10283 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10292 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10294 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10297 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10301 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10305 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10307 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10311 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10312 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10315 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10316 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10320 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10323 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10324 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10331 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10332 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10333 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10335 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10336 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10340 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10345 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10347 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10348 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10349 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10354 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10356 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10360 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10361 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10362 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10363 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10370 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10371 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10377 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10382 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10383 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10389 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10392 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10394 -EBUSY; in kvm_check_and_inject_events()
10398 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10399 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10407 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10409 -EBUSY; in kvm_check_and_inject_events()
10413 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10414 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10419 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10425 -EBUSY; in kvm_check_and_inject_events()
10431 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10442 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10443 kvm_x86_ops.nested_ops->has_events(vcpu, true)) in kvm_check_and_inject_events()
10448 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10450 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10458 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10459 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10463 if (r == -EBUSY) { in kvm_check_and_inject_events()
10484 if (kvm_x86_call(get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10491 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10494 limit--; in process_nmi()
10496 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10497 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10499 if (vcpu->arch.nmi_pending && in process_nmi()
10501 vcpu->arch.nmi_pending--; in process_nmi()
10503 if (vcpu->arch.nmi_pending) in process_nmi()
10510 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10527 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10533 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10540 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10543 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10553 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10558 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10578 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10590 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10595 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10613 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10616 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10619 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10622 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10632 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10634 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10643 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10647 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10648 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10649 else if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10650 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10653 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10660 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10668 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10669 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10675 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10692 * Called within kvm->srcu read side.
10709 r = -EIO; in vcpu_enter_guest()
10719 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10729 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10753 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10754 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10765 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10771 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10774 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10775 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10781 /* Page is swapped out. Do synthetic halt */ in vcpu_enter_guest()
10782 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10799 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10800 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10801 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10802 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10803 vcpu->run->eoi.vector = in vcpu_enter_guest()
10804 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10817 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10818 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10819 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10824 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10825 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10826 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10833 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10834 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10841 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10842 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10859 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { in vcpu_enter_guest()
10868 ++vcpu->stat.req_event; in vcpu_enter_guest()
10874 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10909 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10910 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10915 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10918 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10939 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10955 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10956 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10958 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10960 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10961 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10962 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10963 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10965 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) in vcpu_enter_guest()
10966 kvm_x86_call(set_dr6)(vcpu, vcpu->arch.dr6); in vcpu_enter_guest()
10971 vcpu->arch.host_debugctl = get_debugctlmsr(); in vcpu_enter_guest()
10979 * per-VM state, and responding vCPUs must wait for the update in vcpu_enter_guest()
10998 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
10999 ++vcpu->stat.exits; in vcpu_enter_guest()
11008 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
11009 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
11025 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
11026 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
11028 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11033 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
11036 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
11041 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11046 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
11053 ++vcpu->stat.exits; in vcpu_enter_guest()
11085 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
11088 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
11101 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
11109 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
11110 !vcpu->arch.apf.halted); in kvm_vcpu_running()
11115 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
11122 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
11129 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
11135 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
11153 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
11154 kvm_x86_ops.nested_ops->has_events(vcpu, false)) in kvm_vcpu_has_events()
11168 /* Called within kvm->srcu read side. */
11175 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
11178 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
11186 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
11206 * the halt state to be recorded properly in the VMCS12's activity in vcpu_block()
11207 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
11213 WARN_ON_ONCE(r == -EBUSY); in vcpu_block()
11220 switch(vcpu->arch.mp_state) { in vcpu_block()
11223 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
11224 vcpu->arch.mp_state = in vcpu_block()
11228 vcpu->arch.apf.halted = false; in vcpu_block()
11239 /* Called within kvm->srcu read side. */
11244 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; in vcpu_run()
11253 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11273 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11274 ++vcpu->stat.request_irq_exits; in vcpu_run()
11294 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
11295 * state and halt the vCPU. Exit to userspace if the local APIC is in __kvm_emulate_halt()
11299 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
11302 vcpu->arch.pv.pv_unhalted = false; in __kvm_emulate_halt()
11304 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
11307 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
11322 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
11364 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_preempted_in_kernel()
11369 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
11389 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11414 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11418 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11421 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11422 len = min(8u, frag->len); in complete_emulated_mmio()
11423 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11424 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11426 if (frag->len <= 8) { in complete_emulated_mmio()
11429 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11432 frag->data += len; in complete_emulated_mmio()
11433 frag->gpa += len; in complete_emulated_mmio()
11434 frag->len -= len; in complete_emulated_mmio()
11437 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11438 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11440 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11441 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11443 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11447 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11448 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11449 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11450 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11451 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11452 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11453 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11460 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11461 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11468 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11469 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11475 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11476 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11479 r = kvm_mmu_post_init_vm(vcpu->kvm); in kvm_arch_vcpu_ioctl_run()
11485 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11489 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11490 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11491 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11513 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11515 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11516 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11517 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11522 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || in kvm_arch_vcpu_ioctl_run()
11523 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { in kvm_arch_vcpu_ioctl_run()
11524 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11528 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11534 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11536 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11537 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11544 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11546 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11547 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11548 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11549 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11550 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11551 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11552 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11553 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11555 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11557 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11558 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11559 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11564 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11565 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11568 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11569 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11581 if (kvm_run->kvm_valid_regs) in kvm_arch_vcpu_ioctl_run()
11593 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11601 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11602 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11604 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11605 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11606 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11607 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11608 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11609 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11610 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11611 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11613 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11614 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11615 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11616 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11617 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11618 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11619 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11620 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11623 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11624 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11629 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_regs()
11630 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_regs()
11631 return -EINVAL; in kvm_arch_vcpu_ioctl_get_regs()
11641 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11642 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11644 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11645 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11646 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11647 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11648 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11649 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11650 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11651 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11653 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11654 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11655 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11656 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11657 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11658 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11659 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11660 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11663 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11664 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11666 vcpu->arch.exception.pending = false; in __set_regs()
11667 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11674 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_regs()
11675 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_regs()
11676 return -EINVAL; in kvm_arch_vcpu_ioctl_set_regs()
11688 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11691 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11692 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11693 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11694 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11695 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11696 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11698 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11699 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11702 sregs->idt.limit = dt.size; in __get_sregs_common()
11703 sregs->idt.base = dt.address; in __get_sregs_common()
11705 sregs->gdt.limit = dt.size; in __get_sregs_common()
11706 sregs->gdt.base = dt.address; in __get_sregs_common()
11708 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11709 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11712 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11713 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11714 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11715 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11716 sregs->apic_base = vcpu->arch.apic_base; in __get_sregs_common()
11723 if (vcpu->arch.guest_state_protected) in __get_sregs()
11726 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11727 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11728 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11737 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11742 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11743 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11750 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_sregs()
11751 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_sregs()
11752 return -EINVAL; in kvm_arch_vcpu_ioctl_get_sregs()
11774 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11775 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11776 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11777 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11779 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11791 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11795 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11818 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11819 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11820 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11823 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11824 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11825 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11827 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11839 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11851 if (ret || vcpu->mmio_needed) { in kvm_task_switch()
11852 vcpu->mmio_needed = false; in kvm_task_switch()
11853 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11854 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11855 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11859 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11860 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11867 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11870 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11873 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11875 if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11879 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11880 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11882 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11886 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11887 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11897 return -EINVAL; in __set_sregs_common()
11899 if (kvm_apic_set_base(vcpu, sregs->apic_base, true)) in __set_sregs_common()
11900 return -EINVAL; in __set_sregs_common()
11902 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11905 dt.size = sregs->idt.limit; in __set_sregs_common()
11906 dt.address = sregs->idt.base; in __set_sregs_common()
11908 dt.size = sregs->gdt.limit; in __set_sregs_common()
11909 dt.address = sregs->gdt.base; in __set_sregs_common()
11912 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11913 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11914 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11916 kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11918 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11920 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11921 kvm_x86_call(set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11923 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11924 kvm_x86_call(set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11926 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11927 kvm_x86_call(set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11930 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11935 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11938 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11939 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11940 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11941 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11942 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11943 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11945 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11946 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11952 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11954 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11975 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11988 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
11989 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
11990 !(sregs2->efer & EFER_LMA); in __set_sregs2()
11993 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
11994 return -EINVAL; in __set_sregs2()
11996 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
11997 return -EINVAL; in __set_sregs2()
12006 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
12010 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
12024 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_sregs()
12025 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_sregs()
12026 return -EINVAL; in kvm_arch_vcpu_ioctl_set_sregs()
12043 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12046 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12052 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12056 struct kvm_guest_debug *dbg) in kvm_arch_vcpu_ioctl_set_guest_debug() argument
12061 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
12062 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
12066 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12067 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
12070 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
12082 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
12083 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
12084 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
12086 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12088 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12089 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12092 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12096 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
12097 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
12107 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
12122 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
12128 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
12130 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
12131 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
12132 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
12133 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
12134 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
12144 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
12145 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_get_fpu()
12149 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
12150 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
12151 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
12152 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
12153 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
12154 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
12155 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
12156 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
12157 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
12167 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
12168 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_set_fpu()
12172 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
12174 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
12175 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
12176 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
12177 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
12178 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
12179 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
12180 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
12181 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
12191 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
12192 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
12194 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
12195 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
12197 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
12199 vcpu, &vcpu->run->s.regs.events); in store_regs()
12204 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
12205 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
12206 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
12209 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
12210 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
12213 return -EINVAL; in sync_regs()
12215 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
12218 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
12219 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
12222 return -EINVAL; in sync_regs()
12224 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
12232 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
12236 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12237 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
12239 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12240 return -EINVAL; in kvm_arch_vcpu_precreate()
12250 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
12251 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
12252 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
12254 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm); in kvm_arch_vcpu_create()
12256 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
12257 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
12259 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
12269 r = -ENOMEM; in kvm_arch_vcpu_create()
12274 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
12276 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
12278 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
12280 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
12282 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
12284 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
12291 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
12298 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_STUFF_FEATURE_MSRS)) { in kvm_arch_vcpu_create()
12299 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
12300 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
12301 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
12305 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
12306 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
12309 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
12319 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
12326 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
12328 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
12330 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
12332 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
12333 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
12334 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
12344 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
12346 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12353 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12355 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12357 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12358 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12370 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12371 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12372 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12377 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12378 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12380 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12382 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12383 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12384 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12394 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12404 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12415 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12417 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12418 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12419 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12420 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12421 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12425 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12427 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12428 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12431 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12434 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12435 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12436 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12442 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12444 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12445 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12462 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12464 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_vcpu_reset()
12466 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12467 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12475 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12486 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12493 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12515 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12585 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_enable_virtualization_cpu()
12587 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_enable_virtualization_cpu()
12589 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_enable_virtualization_cpu()
12590 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_enable_virtualization_cpu()
12620 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_enable_virtualization_cpu()
12634 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_enable_virtualization_cpu()
12636 kvm->arch.backwards_tsc_observed = true; in kvm_arch_enable_virtualization_cpu()
12638 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_enable_virtualization_cpu()
12639 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_enable_virtualization_cpu()
12649 kvm->arch.last_tsc_nsec = 0; in kvm_arch_enable_virtualization_cpu()
12650 kvm->arch.last_tsc_write = 0; in kvm_arch_enable_virtualization_cpu()
12665 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12670 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12676 kfree(kvm->arch.hv_pa_pg); in kvm_arch_free_vm()
12688 return -EINVAL; in kvm_arch_init_vm()
12690 kvm->arch.vm_type = type; in kvm_arch_init_vm()
12691 kvm->arch.has_private_mem = in kvm_arch_init_vm()
12694 kvm->arch.pre_fault_allowed = in kvm_arch_init_vm()
12707 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12708 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12711 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12712 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12714 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12716 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12717 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12718 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12719 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12721 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12723 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12725 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12726 kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT; in kvm_arch_init_vm()
12727 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12728 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12731 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12732 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12735 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12736 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12749 once_init(&kvm->arch.nx_once); in kvm_arch_init_vm()
12779 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12780 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12797 * -errno: on error
12802 * GPA->HVA translation will not change. However, the HVA is a user
12814 lockdep_assert_held(&kvm->slots_lock); in __x86_set_memory_region()
12817 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12821 if (slot && slot->npages) in __x86_set_memory_region()
12822 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12833 if (!slot || !slot->npages) in __x86_set_memory_region()
12836 old_npages = slot->npages; in __x86_set_memory_region()
12837 hva = slot->userspace_addr; in __x86_set_memory_region()
12867 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12873 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12879 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12884 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12887 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12888 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12900 vfree(slot->arch.rmap[i]); in memslot_rmap_free()
12901 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12912 vfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12913 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12921 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12928 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12931 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12932 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12934 return -ENOMEM; in memslot_rmap_alloc()
12944 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12952 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12972 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12974 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12976 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12977 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12978 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12983 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
13004 vfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
13005 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
13007 return -ENOMEM; in kvm_alloc_memslot_metadata()
13016 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
13021 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
13036 return -EINVAL; in kvm_arch_prepare_memory_region()
13039 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
13040 return -EINVAL; in kvm_arch_prepare_memory_region()
13042 if (kvm_is_gfn_alias(kvm, new->base_gfn + new->npages - 1)) in kvm_arch_prepare_memory_region()
13043 return -EINVAL; in kvm_arch_prepare_memory_region()
13049 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
13051 return -EIO; in kvm_arch_prepare_memory_region()
13064 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
13074 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
13075 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
13095 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
13104 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
13124 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
13149 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
13156 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
13165 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
13166 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
13167 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
13168 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
13170 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
13173 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
13174 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
13177 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
13180 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
13194 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
13198 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
13214 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
13224 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_get_ip()
13243 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
13264 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
13272 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
13273 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13294 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13301 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13304 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13313 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13314 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13322 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13331 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13335 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13338 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13340 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13347 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13356 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13364 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13373 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13386 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13395 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13413 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13418 * halt state. in kvm_can_do_async_pf()
13428 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13429 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13437 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13444 * page fault, but putting the guest in an artificial halt state in kvm_arch_async_page_not_present()
13460 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13463 if (work->wakeup_all) in kvm_arch_async_page_present()
13464 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13466 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13467 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13469 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13471 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13472 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13476 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13477 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13483 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13497 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13504 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13510 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13517 * Non-coherent DMA assignment and de-assignment may affect whether or in kvm_noncoherent_dma_assignment_start_or_stop()
13520 * (or last) non-coherent device is (un)registered to so that new SPTEs in kvm_noncoherent_dma_assignment_start_or_stop()
13529 if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) in kvm_arch_register_noncoherent_dma()
13536 if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) in kvm_arch_unregister_noncoherent_dma()
13543 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13559 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13560 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13561 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13562 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13564 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13576 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13577 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13581 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13585 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_del_producer()
13586 prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13589 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13591 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13603 if (new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13606 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13616 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13662 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13668 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13670 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13681 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13694 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13695 return -EIO; in kvm_handle_memory_failure()
13705 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13761 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13780 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13784 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13787 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13788 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13789 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13790 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13792 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13795 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13798 frag->data += len; in complete_sev_es_emulated_mmio()
13799 frag->gpa += len; in complete_sev_es_emulated_mmio()
13800 frag->len -= len; in complete_sev_es_emulated_mmio()
13803 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13804 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13812 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13813 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13814 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13815 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13816 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13817 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13819 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13831 return -EINVAL; in kvm_sev_es_mmio_write()
13837 bytes -= handled; in kvm_sev_es_mmio_write()
13842 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13843 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13844 frag->len = bytes; in kvm_sev_es_mmio_write()
13845 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13846 frag->data = data; in kvm_sev_es_mmio_write()
13848 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13849 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13851 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13852 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13853 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13854 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13855 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13857 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13870 return -EINVAL; in kvm_sev_es_mmio_read()
13876 bytes -= handled; in kvm_sev_es_mmio_read()
13881 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13882 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13883 frag->len = bytes; in kvm_sev_es_mmio_read()
13884 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13885 frag->data = data; in kvm_sev_es_mmio_read()
13887 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13888 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13890 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13891 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13892 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13893 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13895 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13903 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13904 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13912 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13913 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13915 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13916 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13926 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13927 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13935 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13939 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13948 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13949 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13950 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13952 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13954 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13964 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13965 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13970 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13974 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13982 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13983 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()