Lines Matching +full:dram +full:- +full:access +full:- +full:quirk

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
16 * Ben-Ami Yassour <[email protected]>
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()
964 * else the access will fault indefinitely (and to emulate hardware). 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(&gtod->seq); in do_kvmclock_base()
2831 ns = gtod->raw_clock.base_cycles; in do_kvmclock_base()
2832 ns += vgettsc(&gtod->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(&gtod->seq, seq))); in do_kvmclock_base()
2853 seq = read_seqcount_begin(&gtod->seq); in do_monotonic()
2854 ns = gtod->clock.base_cycles; in do_monotonic()
2855 ns += vgettsc(&gtod->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(&gtod->seq, seq))); in do_monotonic()
2872 seq = read_seqcount_begin(&gtod->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(&gtod->clock, tsc_timestamp, &mode); in do_realtime()
2876 ns >>= gtod->clock.shift; in do_realtime()
2877 } while (unlikely(read_seqcount_retry(&gtod->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()
4200 case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ 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()
4578 return kvm && kvm->arch.has_protected_state ? 0 : KVM_SYNC_X86_VALID_FIELDS; in kvm_sync_valid_fields()
4737 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4738 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4745 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4790 if (attr->group) { in __kvm_x86_dev_get_attr()
4792 return kvm_x86_call(dev_get_attr)(attr->group, attr->attr, val); in __kvm_x86_dev_get_attr()
4793 return -ENXIO; in __kvm_x86_dev_get_attr()
4796 switch (attr->attr) { in __kvm_x86_dev_get_attr()
4801 return -ENXIO; in __kvm_x86_dev_get_attr()
4807 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_x86_dev_get_attr()
4816 return -EFAULT; in kvm_x86_dev_get_attr()
4840 r = -EFAULT; in kvm_arch_dev_ioctl()
4847 r = -E2BIG; in kvm_arch_dev_ioctl()
4850 r = -EFAULT; in kvm_arch_dev_ioctl()
4851 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4854 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4866 r = -EFAULT; in kvm_arch_dev_ioctl()
4870 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4875 r = -EFAULT; in kvm_arch_dev_ioctl()
4882 r = -EFAULT; in kvm_arch_dev_ioctl()
4893 r = -EFAULT; in kvm_arch_dev_ioctl()
4900 r = -E2BIG; in kvm_arch_dev_ioctl()
4903 r = -EFAULT; in kvm_arch_dev_ioctl()
4904 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4920 r = -EFAULT; in kvm_arch_dev_ioctl()
4928 r = -EFAULT; in kvm_arch_dev_ioctl()
4935 r = -EINVAL; in kvm_arch_dev_ioctl()
4949 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4956 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_vcpu_load()
4958 if (vcpu->scheduled_out && pmu->version && pmu->event_count) { in kvm_arch_vcpu_load()
4959 pmu->need_cleanup = true; in kvm_arch_vcpu_load()
4966 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4967 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4968 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
4975 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
4978 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
4979 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
4980 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
4984 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
4985 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
4986 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
4992 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
4994 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
5002 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
5004 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
5006 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5008 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
5016 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
5020 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
5023 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
5027 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
5029 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
5030 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
5034 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
5035 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
5038 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
5042 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
5045 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
5047 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
5048 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
5049 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
5052 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
5053 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
5055 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
5056 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
5058 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
5065 if (vcpu->preempted) { in kvm_arch_vcpu_put()
5067 * Assume protected guests are in-kernel. Inefficient yielding in kvm_arch_vcpu_put()
5071 vcpu->arch.preempted_in_kernel = vcpu->arch.guest_state_protected || in kvm_arch_vcpu_put()
5078 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
5079 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
5083 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
5087 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
5134 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
5145 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
5146 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
5148 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
5149 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5155 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5156 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5158 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5159 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5161 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5162 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5164 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5179 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5180 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5181 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5191 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5197 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5200 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5203 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5205 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5218 * - none of the bits for Machine Check Exceptions are set
5219 * - both the VAL (valid) and UC (uncorrectable) bits are set
5220 * MCI_STATUS_PCC - Processor Context Corrupted
5221 * MCI_STATUS_S - Signaled as a Machine Check Exception
5222 * MCI_STATUS_AR - Software recoverable Action Required
5226 return !mce->mcg_status && in is_ucna()
5227 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5228 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5229 (mce->status & MCI_STATUS_UC); in is_ucna()
5234 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5236 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5237 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5238 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5239 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5242 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5246 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5254 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5256 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5258 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5259 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5261 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5270 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5271 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5277 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5279 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5280 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5286 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5287 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5288 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5289 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5290 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5295 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5296 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5297 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5298 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5319 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5320 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5323 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5324 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5325 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5326 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5328 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5333 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5338 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5339 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5350 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5351 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5352 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5358 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5359 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5361 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5362 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5363 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5364 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5365 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5367 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5368 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5369 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5370 events->interrupt.shadow = kvm_x86_call(get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5372 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5373 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5374 events->nmi.masked = kvm_x86_call(get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5376 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5379 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5380 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5381 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5382 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5384 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5386 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5389 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5390 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5391 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5392 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5393 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5400 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5406 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5408 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5409 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5410 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5411 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5412 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5414 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5416 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5417 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5420 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5421 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5422 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5425 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5426 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5427 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5428 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5434 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5435 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5438 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5440 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5442 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5444 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5445 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5446 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5447 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5448 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5449 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5450 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5452 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5453 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5454 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5455 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5457 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5459 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5460 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5461 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5462 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5463 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5466 kvm_x86_call(set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5468 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5470 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5472 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5474 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5476 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5479 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5481 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5482 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5483 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5485 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5489 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5490 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5491 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5495 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5496 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5498 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5502 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5503 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5504 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5505 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5521 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_debugregs()
5522 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_debugregs()
5523 return -EINVAL; in kvm_vcpu_ioctl_x86_get_debugregs()
5527 BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.db) != ARRAY_SIZE(dbgregs->db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5528 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_get_debugregs()
5529 dbgregs->db[i] = vcpu->arch.db[i]; in kvm_vcpu_ioctl_x86_get_debugregs()
5531 dbgregs->dr6 = vcpu->arch.dr6; in kvm_vcpu_ioctl_x86_get_debugregs()
5532 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5541 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_debugregs()
5542 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_debugregs()
5543 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5545 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5546 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5548 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5549 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5550 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5551 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5553 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_set_debugregs()
5554 vcpu->arch.db[i] = dbgregs->db[i]; in kvm_vcpu_ioctl_x86_set_debugregs()
5557 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5558 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5580 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5583 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5584 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_get_xsave2()
5586 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5587 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5594 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5595 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5601 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5602 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_set_xsave()
5604 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5605 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5607 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5613 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_xcrs()
5614 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_xcrs()
5615 return -EINVAL; in kvm_vcpu_ioctl_x86_get_xcrs()
5618 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5622 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5623 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5624 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5625 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5634 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_xcrs()
5635 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_xcrs()
5636 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5639 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5641 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5642 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5644 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5646 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5648 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5652 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5664 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5665 return -EINVAL; in kvm_set_guest_paused()
5666 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5676 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5681 r = -ENXIO; in kvm_arch_tsc_has_attr()
5690 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_get_attr()
5693 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5695 r = -EFAULT; in kvm_arch_tsc_get_attr()
5696 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5701 r = -ENXIO; in kvm_arch_tsc_get_attr()
5710 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_set_attr()
5711 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5714 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5720 r = -EFAULT; in kvm_arch_tsc_set_attr()
5724 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5726 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5727 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5728 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5730 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5733 kvm->arch.user_set_tsc = true; in kvm_arch_tsc_set_attr()
5735 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5741 r = -ENXIO; in kvm_arch_tsc_set_attr()
5755 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5758 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5778 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5779 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5781 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5784 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5785 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5789 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5790 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5791 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5799 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5800 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5801 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5803 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5806 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5812 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5817 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5821 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5824 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5831 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5847 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5852 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5858 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5865 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5880 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5898 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5901 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5908 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5912 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5919 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5923 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5926 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5933 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5935 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5939 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5941 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5947 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5953 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5963 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5966 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5969 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5971 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5977 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5986 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5997 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6006 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6022 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6032 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6041 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6042 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
6046 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6054 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6061 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6074 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6077 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6085 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6095 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6103 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6123 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6139 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
6149 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6159 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6160 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
6163 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
6164 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6165 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6168 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
6174 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6175 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6177 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6189 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6190 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6193 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6197 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6212 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6213 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6214 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6226 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6231 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6237 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6245 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6246 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6247 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6251 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6255 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6262 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6263 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6264 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6282 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6300 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6301 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6316 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6318 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6321 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6323 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6329 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6333 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6335 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6339 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6343 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6346 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6354 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6358 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6360 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6361 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6363 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6366 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6367 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6369 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6372 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6375 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6384 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6386 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6388 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6389 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6390 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6397 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6399 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6400 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6402 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6403 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6409 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6410 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6411 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6412 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6413 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6414 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6423 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6425 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6426 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6427 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6430 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6431 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6432 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6434 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6436 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6443 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6445 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6449 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6450 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6451 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6462 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6463 * VM-Exit. in kvm_arch_sync_dirty_log()
6479 return -ENXIO; in kvm_vm_ioctl_irq_line()
6481 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6482 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6492 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6493 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6495 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6497 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6498 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6502 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6506 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6507 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6508 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6510 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6513 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6517 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6518 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6522 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6526 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6527 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6530 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6531 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6532 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6533 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6538 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6539 if (cap->args[0] & ~kvm_get_allowed_disable_exits()) in kvm_vm_ioctl_enable_cap()
6542 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6543 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6546 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6551 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6554 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6555 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6556 if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) in kvm_vm_ioctl_enable_cap()
6557 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6558 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6559 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6560 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6561 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6564 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6567 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6571 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6575 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6579 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6580 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6582 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6586 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6587 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6590 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6591 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6595 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6596 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6603 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6610 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6612 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6617 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6621 r = kvm_x86_call(vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6624 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6628 r = kvm_x86_call(vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6631 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6632 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6635 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6639 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6640 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6642 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6646 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6647 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6650 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6651 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6652 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6655 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6658 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6659 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6662 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6663 if (kvm->arch.bsp_vcpu_id > cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6665 } else if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6667 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6668 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6671 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6674 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6675 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6679 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6681 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6682 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6683 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6684 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6687 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6690 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6704 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6708 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6711 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6712 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6713 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6716 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6719 u64 bus_cycle_ns = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6726 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6732 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6734 r = -ENXIO; in kvm_vm_ioctl_enable_cap()
6735 else if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6736 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6738 kvm->arch.apic_bus_cycle_ns = bus_cycle_ns; in kvm_vm_ioctl_enable_cap()
6739 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6743 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6757 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6768 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6769 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6780 if (!user_range->nmsrs) in kvm_add_msr_filter()
6783 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6784 return -EINVAL; in kvm_add_msr_filter()
6786 if (!user_range->flags) in kvm_add_msr_filter()
6787 return -EINVAL; in kvm_add_msr_filter()
6789 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6791 return -EINVAL; in kvm_add_msr_filter()
6793 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6797 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6798 .flags = user_range->flags, in kvm_add_msr_filter()
6799 .base = user_range->base, in kvm_add_msr_filter()
6800 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6804 msr_filter->count++; in kvm_add_msr_filter()
6817 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6818 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6820 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6821 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6823 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6825 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6829 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6831 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6832 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6839 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6840 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6841 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6842 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6843 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6872 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6873 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6884 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6892 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6893 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6894 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6895 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6915 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6917 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6923 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6927 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6950 return -EFAULT; in kvm_vm_ioctl_get_clock()
6957 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6962 return -EFAULT; in kvm_vm_ioctl_set_clock()
6969 return -EINVAL; in kvm_vm_ioctl_set_clock()
6989 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
6992 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
6993 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
6996 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
7003 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
7005 int r = -ENOTTY; in kvm_arch_vm_ioctl()
7007 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
7024 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7025 r = -EINVAL; in kvm_arch_vm_ioctl()
7026 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7028 r = -EFAULT; in kvm_arch_vm_ioctl()
7033 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7040 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7042 r = -EEXIST; in kvm_arch_vm_ioctl()
7046 r = -EINVAL; in kvm_arch_vm_ioctl()
7047 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7066 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
7068 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
7071 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7078 r = -EFAULT; in kvm_arch_vm_ioctl()
7083 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7084 r = -EEXIST; in kvm_arch_vm_ioctl()
7085 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7087 r = -ENOENT; in kvm_arch_vm_ioctl()
7090 r = -ENOMEM; in kvm_arch_vm_ioctl()
7091 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
7092 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7095 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7107 r = -ENXIO; in kvm_arch_vm_ioctl()
7113 r = -EFAULT; in kvm_arch_vm_ioctl()
7131 r = -ENXIO; in kvm_arch_vm_ioctl()
7140 r = -EFAULT; in kvm_arch_vm_ioctl()
7143 r = -ENXIO; in kvm_arch_vm_ioctl()
7144 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7149 r = -EFAULT; in kvm_arch_vm_ioctl()
7156 r = -EFAULT; in kvm_arch_vm_ioctl()
7159 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7160 r = -ENXIO; in kvm_arch_vm_ioctl()
7161 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7165 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7169 r = -ENXIO; in kvm_arch_vm_ioctl()
7170 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7175 r = -EFAULT; in kvm_arch_vm_ioctl()
7182 r = -EFAULT; in kvm_arch_vm_ioctl()
7185 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7186 r = -ENXIO; in kvm_arch_vm_ioctl()
7187 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7191 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7196 r = -EFAULT; in kvm_arch_vm_ioctl()
7199 r = -ENXIO; in kvm_arch_vm_ioctl()
7200 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7207 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7208 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7209 r = -EBUSY; in kvm_arch_vm_ioctl()
7211 (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) in kvm_arch_vm_ioctl()
7212 r = -EINVAL; in kvm_arch_vm_ioctl()
7214 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
7215 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7220 r = -EFAULT; in kvm_arch_vm_ioctl()
7229 r = -EFAULT; in kvm_arch_vm_ioctl()
7234 r = -EFAULT; in kvm_arch_vm_ioctl()
7240 r = -EFAULT; in kvm_arch_vm_ioctl()
7249 r = -EFAULT; in kvm_arch_vm_ioctl()
7265 r = -EINVAL; in kvm_arch_vm_ioctl()
7275 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7281 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7285 r = -ENOTTY; in kvm_arch_vm_ioctl()
7295 r = -EFAULT; in kvm_arch_vm_ioctl()
7299 r = -ENOTTY; in kvm_arch_vm_ioctl()
7309 r = -EFAULT; in kvm_arch_vm_ioctl()
7313 r = -ENOTTY; in kvm_arch_vm_ioctl()
7324 r = -EFAULT; in kvm_arch_vm_ioctl()
7339 return -EFAULT; in kvm_arch_vm_ioctl()
7345 r = -ENOTTY; in kvm_arch_vm_ioctl()
7405 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7410 MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7411 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7416 MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7417 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7422 MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1: in kvm_probe_msr_to_save()
7423 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7492 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7497 len -= n; in vcpu_mmio_write()
7512 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7519 len -= n; in vcpu_mmio_read()
7538 gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 access, in translate_nested_gpa() argument
7541 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7546 /* NPT walks are always user-walks */ in translate_nested_gpa()
7547 access |= PFERR_USER_MASK; in translate_nested_gpa()
7548 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7556 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7558 u64 access = (kvm_x86_call(get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_mmu_gva_to_gpa_read() local
7559 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7566 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7568 u64 access = (kvm_x86_call(get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_mmu_gva_to_gpa_write() local
7569 access |= PFERR_WRITE_MASK; in kvm_mmu_gva_to_gpa_write()
7570 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7574 /* uses this to access any guest's mapped memory without checking CPL */
7578 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7580 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7584 struct kvm_vcpu *vcpu, u64 access, in kvm_read_guest_virt_helper() argument
7587 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7592 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7593 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7594 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7606 bytes -= toread; in kvm_read_guest_virt_helper()
7620 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7621 u64 access = (kvm_x86_call(get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_fetch_guest_virt() local
7626 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7631 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7633 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7646 u64 access = (kvm_x86_call(get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0; in kvm_read_guest_virt() local
7655 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, in kvm_read_guest_virt()
7665 u64 access = 0; in emulator_read_std() local
7668 access |= PFERR_IMPLICIT_ACCESS; in emulator_read_std()
7670 access |= PFERR_USER_MASK; in emulator_read_std()
7672 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); in emulator_read_std()
7676 struct kvm_vcpu *vcpu, u64 access, in kvm_write_guest_virt_helper() argument
7679 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7684 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7685 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7686 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7697 bytes -= towrite; in kvm_write_guest_virt_helper()
7710 u64 access = PFERR_WRITE_MASK; in emulator_write_std() local
7713 access |= PFERR_IMPLICIT_ACCESS; in emulator_write_std()
7715 access |= PFERR_USER_MASK; in emulator_write_std()
7718 access, exception); in emulator_write_std()
7725 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7769 /* For APIC access vmexit */ in vcpu_is_mmio_gpa()
7785 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7786 u64 access = ((kvm_x86_call(get_cpl)(vcpu) == 3) ? PFERR_USER_MASK : 0) in vcpu_mmio_gva_to_gpa() local
7795 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7796 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7797 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7798 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7803 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7806 return -1; in vcpu_mmio_gva_to_gpa()
7837 if (vcpu->mmio_read_completed) { in read_prepare()
7839 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7840 vcpu->mmio_read_completed = 0; in read_prepare()
7875 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7877 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7903 bool write = ops->write; in emulator_read_write_onepage()
7905 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7914 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7915 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7916 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7924 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7930 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7935 bytes -= handled; in emulator_read_write_onepage()
7938 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7939 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7940 frag->gpa = gpa; in emulator_read_write_onepage()
7941 frag->data = val; in emulator_read_write_onepage()
7942 frag->len = bytes; in emulator_read_write_onepage()
7956 if (ops->read_write_prepare && in emulator_read_write()
7957 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7960 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7963 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7966 now = -addr & ~PAGE_MASK; in emulator_read_write()
7973 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
7976 bytes -= now; in emulator_read_write()
7984 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
7987 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
7989 vcpu->mmio_needed = 1; in emulator_read_write()
7990 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
7992 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
7993 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
7994 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
7995 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
7997 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
8037 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
8048 * enabled in the host and the access splits a cache line. in emulator_cmpxchg_emulated()
8051 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
8055 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
8088 * back the original value and the access is atomic, but KVM's ABI is in emulator_cmpxchg_emulated()
8113 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
8129 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
8138 vcpu->arch.pio.port = port; in emulator_pio_in_out()
8139 vcpu->arch.pio.in = in; in emulator_pio_in_out()
8140 vcpu->arch.pio.count = count; in emulator_pio_in_out()
8141 vcpu->arch.pio.size = size; in emulator_pio_in_out()
8144 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
8146 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
8148 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
8149 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
8150 vcpu->run->io.size = size; in emulator_pio_in_out()
8151 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
8152 vcpu->run->io.count = count; in emulator_pio_in_out()
8153 vcpu->run->io.port = port; in emulator_pio_in_out()
8169 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
8170 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
8171 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
8172 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
8173 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
8181 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
8229 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8230 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
8233 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8267 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8280 value = vcpu->arch.cr2; in emulator_get_cr()
8309 vcpu->arch.cr2 = val; in emulator_set_cr()
8322 res = -1; in emulator_set_cr()
8383 desc->type = var.type; in emulator_get_segment()
8384 desc->s = var.s; in emulator_get_segment()
8385 desc->dpl = var.dpl; in emulator_get_segment()
8386 desc->p = var.present; in emulator_get_segment()
8387 desc->avl = var.avl; in emulator_get_segment()
8388 desc->l = var.l; in emulator_get_segment()
8389 desc->d = var.db; in emulator_get_segment()
8390 desc->g = var.g; in emulator_get_segment()
8408 if (desc->g) in emulator_set_segment()
8410 var.type = desc->type; in emulator_set_segment()
8411 var.dpl = desc->dpl; in emulator_set_segment()
8412 var.db = desc->d; in emulator_set_segment()
8413 var.s = desc->s; in emulator_set_segment()
8414 var.l = desc->l; in emulator_set_segment()
8415 var.g = desc->g; in emulator_set_segment()
8416 var.avl = desc->avl; in emulator_set_segment()
8417 var.present = desc->p; in emulator_set_segment()
8490 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8498 &ctxt->exception); in emulator_intercept()
8573 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8575 if (!kvm->vm_bugged) in emulator_vm_bugged()
8665 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8667 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8668 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8669 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8670 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8671 ctxt->exception.error_code); in inject_emulated_exception()
8673 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8686 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8687 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8688 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8695 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8700 ctxt->gpa_available = false; in init_emulate_ctxt()
8701 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8702 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8704 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8705 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8706 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8710 ctxt->interruptibility = 0; in init_emulate_ctxt()
8711 ctxt->have_exception = false; in init_emulate_ctxt()
8712 ctxt->exception.vector = -1; in init_emulate_ctxt()
8713 ctxt->perm_ok = false; in init_emulate_ctxt()
8716 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8721 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8726 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8727 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8728 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8734 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8735 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8736 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8744 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8757 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8758 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8770 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8773 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8774 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8776 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8778 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8779 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8780 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8781 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8784 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8785 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8788 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8793 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8795 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8796 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8815 struct kvm_run *run = vcpu->run; in kvm_prepare_event_vectoring_exit()
8822 run->internal.data[ndata++] = info2; in kvm_prepare_event_vectoring_exit()
8823 run->internal.data[ndata++] = reason; in kvm_prepare_event_vectoring_exit()
8824 run->internal.data[ndata++] = info1; in kvm_prepare_event_vectoring_exit()
8825 run->internal.data[ndata++] = gpa; in kvm_prepare_event_vectoring_exit()
8826 run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu; in kvm_prepare_event_vectoring_exit()
8828 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_prepare_event_vectoring_exit()
8829 run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; in kvm_prepare_event_vectoring_exit()
8830 run->internal.ndata = ndata; in kvm_prepare_event_vectoring_exit()
8836 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8838 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8846 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8870 * If the failed instruction faulted on an access to page tables that in kvm_unprotect_and_retry_on_failure()
8875 * a SPTE and write-protect the gfn to resolve the !PRESENT fault, and in kvm_unprotect_and_retry_on_failure()
8884 * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the in kvm_unprotect_and_retry_on_failure()
8885 * guest to let the CPU re-execute the instruction in the hope that the in kvm_unprotect_and_retry_on_failure()
8920 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8922 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8923 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8924 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8925 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8926 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8996 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8997 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8998 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
9001 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
9002 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
9005 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
9006 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
9007 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
9008 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
9014 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
9018 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
9019 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
9033 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
9035 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9052 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9065 * (and wrong) when emulating on an intercepted fault-like exception[*], as
9075 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
9083 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
9093 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
9119 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
9126 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
9144 if (ctxt->have_exception && in x86_emulate_instruction()
9147 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
9150 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
9151 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
9169 * injecting single-step #DBs. in x86_emulate_instruction()
9172 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
9173 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
9175 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
9182 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9183 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
9184 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
9189 * If emulation was caused by a write-protection #PF on a non-page_table in x86_emulate_instruction()
9201 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
9202 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
9209 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
9212 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
9213 ctxt->gpa_available = true; in x86_emulate_instruction()
9214 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
9218 ctxt->exception.address = 0; in x86_emulate_instruction()
9234 if (ctxt->have_exception) { in x86_emulate_instruction()
9235 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9236 vcpu->mmio_needed = false; in x86_emulate_instruction()
9239 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9240 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9241 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9242 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9245 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9248 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9249 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9251 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9254 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9255 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9266 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9267 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9270 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9272 * of which are fault-like. in x86_emulate_instruction()
9274 if (!ctxt->have_exception || in x86_emulate_instruction()
9275 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9277 if (ctxt->is_branch) in x86_emulate_instruction()
9279 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9280 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9283 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9292 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9295 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9315 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9321 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9323 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9343 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9344 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9348 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9349 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9359 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9361 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9362 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9367 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9390 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9391 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9422 khz = freq->new; in tsc_khz_changed()
9443 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9511 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9514 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9520 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9543 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9545 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9548 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9576 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9577 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9635 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9648 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9658 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9661 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9679 return -EIO; in kvm_x86_check_processor_compatibility()
9698 return -EEXIST; in kvm_x86_vendor_init()
9708 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9713 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9726 return -EIO; in kvm_x86_vendor_init()
9734 return -ENOMEM; in kvm_x86_vendor_init()
9740 r = -ENOMEM; in kvm_x86_vendor_init()
9762 kvm_init_pmu_capability(ops->pmu_ops); in kvm_x86_vendor_init()
9767 r = ops->hardware_setup(); in kvm_x86_vendor_init()
9786 if (pi_inject_timer == -1) in kvm_x86_vendor_init()
9795 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in kvm_x86_vendor_init()
9875 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9881 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9882 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9885 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9894 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9896 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9905 * @apicid - apicid of vcpu to be kicked.
9925 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9931 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9959 set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason, true); in kvm_apicv_init()
9961 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9969 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9975 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9977 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9978 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9982 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9992 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
10000 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
10016 ++vcpu->stat.hypercalls; in ____kvm_emulate_hypercall()
10029 ret = -KVM_EPERM; in ____kvm_emulate_hypercall()
10033 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10043 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in ____kvm_emulate_hypercall()
10056 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in ____kvm_emulate_hypercall()
10068 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10069 if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) in ____kvm_emulate_hypercall()
10074 ret = -KVM_EINVAL; in ____kvm_emulate_hypercall()
10078 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in ____kvm_emulate_hypercall()
10079 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in ____kvm_emulate_hypercall()
10081 * In principle this should have been -KVM_ENOSYS, but userspace (QEMU <=9.2) in ____kvm_emulate_hypercall()
10082 * assumed that vcpu->run->hypercall.ret is never changed by KVM and thus that in ____kvm_emulate_hypercall()
10084 * vcpu->run->hypercall.ret, ensuring that it is zero to not break QEMU. in ____kvm_emulate_hypercall()
10086 vcpu->run->hypercall.ret = 0; in ____kvm_emulate_hypercall()
10087 vcpu->run->hypercall.args[0] = gpa; in ____kvm_emulate_hypercall()
10088 vcpu->run->hypercall.args[1] = npages; in ____kvm_emulate_hypercall()
10089 vcpu->run->hypercall.args[2] = attrs; in ____kvm_emulate_hypercall()
10090 vcpu->run->hypercall.flags = 0; in ____kvm_emulate_hypercall()
10092 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in ____kvm_emulate_hypercall()
10094 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in ____kvm_emulate_hypercall()
10095 vcpu->arch.complete_userspace_io = complete_hypercall; in ____kvm_emulate_hypercall()
10099 ret = -KVM_ENOSYS; in ____kvm_emulate_hypercall()
10104 vcpu->run->hypercall.ret = ret; in ____kvm_emulate_hypercall()
10111 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
10131 * If the quirk is disabled, synthesize a #UD and let the guest pick up in emulator_fix_hypercall()
10134 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
10135 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
10136 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
10137 ctxt->have_exception = true; in emulator_fix_hypercall()
10144 &ctxt->exception); in emulator_fix_hypercall()
10149 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
10150 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
10153 /* Called within kvm->srcu read side. */
10156 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
10158 kvm_run->if_flag = kvm_x86_call(get_if_flag)(vcpu); in post_kvm_run_save()
10159 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
10160 kvm_run->apic_base = vcpu->arch.apic_base; in post_kvm_run_save()
10162 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
10163 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
10167 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
10169 kvm_run->flags |= KVM_RUN_X86_GUEST_MODE; in post_kvm_run_save()
10182 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10185 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10188 max_irr = -1; in update_cr8_intercept()
10190 if (max_irr != -1) in update_cr8_intercept()
10202 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10206 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10215 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10218 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10220 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10221 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10222 vcpu->arch.exception.error_code, in kvm_inject_exception()
10223 vcpu->arch.exception.injected); in kvm_inject_exception()
10233 * injected as part of a previous VM-Enter, but weren't successfully delivered
10234 * and need to be re-injected.
10239 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10240 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10245 * instruction boundaries for asynchronous events. However, because VM-Exits
10251 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10274 * Process nested events first, as nested VM-Exit supersedes event in kvm_check_and_inject_events()
10275 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10276 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10284 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10288 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10297 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10299 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10302 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10306 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10310 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10312 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10316 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10317 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10320 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10321 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10325 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10328 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10329 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10336 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10337 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10338 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10340 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10341 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10345 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10350 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10352 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10353 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10354 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10359 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10361 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10365 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10366 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10367 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10368 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10375 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10376 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10382 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10387 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10388 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10394 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10397 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10399 -EBUSY; in kvm_check_and_inject_events()
10403 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10404 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10412 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10414 -EBUSY; in kvm_check_and_inject_events()
10418 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10419 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10424 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10430 -EBUSY; in kvm_check_and_inject_events()
10436 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10447 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10448 kvm_x86_ops.nested_ops->has_events(vcpu, true)) in kvm_check_and_inject_events()
10453 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10455 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10463 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10464 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10468 if (r == -EBUSY) { in kvm_check_and_inject_events()
10489 if (kvm_x86_call(get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10496 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10499 limit--; in process_nmi()
10501 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10502 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10504 if (vcpu->arch.nmi_pending && in process_nmi()
10506 vcpu->arch.nmi_pending--; in process_nmi()
10508 if (vcpu->arch.nmi_pending) in process_nmi()
10515 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10532 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10538 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10545 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10548 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10558 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10563 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10579 * the vCPU would incorrectly be able to access the vAPIC page via MMIO in kvm_vcpu_update_apicv()
10581 * access page is sticky. in kvm_vcpu_update_apicv()
10583 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10595 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10600 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10618 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10621 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10624 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10627 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10637 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10639 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10648 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10652 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10653 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10654 else if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10655 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10658 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10665 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10673 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10674 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10680 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10697 * Called within kvm->srcu read side.
10714 r = -EIO; in vcpu_enter_guest()
10724 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10734 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10758 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10759 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10770 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10776 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10779 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10780 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10787 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10804 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10805 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10806 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10807 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10808 vcpu->run->eoi.vector = in vcpu_enter_guest()
10809 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10822 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10823 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10824 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10829 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10830 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10831 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10838 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10839 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10846 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10847 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10864 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { in vcpu_enter_guest()
10873 ++vcpu->stat.req_event; in vcpu_enter_guest()
10879 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10914 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10915 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10920 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10923 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10944 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10960 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10961 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10963 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10965 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10966 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10967 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10968 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10970 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) in vcpu_enter_guest()
10971 kvm_x86_call(set_dr6)(vcpu, vcpu->arch.dr6); in vcpu_enter_guest()
10976 vcpu->arch.host_debugctl = get_debugctlmsr(); in vcpu_enter_guest()
10984 * per-VM state, and responding vCPUs must wait for the update in vcpu_enter_guest()
11003 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
11004 ++vcpu->stat.exits; in vcpu_enter_guest()
11009 * since we do this before handling the vmexit, a DR access vmexit in vcpu_enter_guest()
11013 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
11014 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
11030 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
11031 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
11033 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11038 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
11041 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
11046 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11051 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
11058 ++vcpu->stat.exits; in vcpu_enter_guest()
11090 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
11093 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
11106 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
11114 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
11115 !vcpu->arch.apf.halted); in kvm_vcpu_running()
11120 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
11127 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
11134 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
11140 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
11158 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
11159 kvm_x86_ops.nested_ops->has_events(vcpu, false)) in kvm_vcpu_has_events()
11173 /* Called within kvm->srcu read side. */
11180 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
11183 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
11191 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
11212 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
11218 WARN_ON_ONCE(r == -EBUSY); in vcpu_block()
11225 switch(vcpu->arch.mp_state) { in vcpu_block()
11228 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
11229 vcpu->arch.mp_state = in vcpu_block()
11233 vcpu->arch.apf.halted = false; in vcpu_block()
11244 /* Called within kvm->srcu read side. */
11249 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; in vcpu_run()
11258 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11278 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11279 ++vcpu->stat.request_irq_exits; in vcpu_run()
11299 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
11304 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
11307 vcpu->arch.pv.pv_unhalted = false; in __kvm_emulate_halt()
11309 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
11312 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
11327 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
11369 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_preempted_in_kernel()
11374 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
11394 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11419 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11423 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11426 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11427 len = min(8u, frag->len); in complete_emulated_mmio()
11428 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11429 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11431 if (frag->len <= 8) { in complete_emulated_mmio()
11434 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11437 frag->data += len; in complete_emulated_mmio()
11438 frag->gpa += len; in complete_emulated_mmio()
11439 frag->len -= len; in complete_emulated_mmio()
11442 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11443 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11445 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11446 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11448 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11452 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11453 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11454 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11455 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11456 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11457 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11458 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11465 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11466 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11473 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11474 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11480 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11481 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11485 r = kvm_mmu_post_init_vm(vcpu->kvm); in kvm_arch_vcpu_ioctl_run()
11491 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11495 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11496 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11497 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11519 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11521 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11522 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11523 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11528 sync_valid_fields = kvm_sync_valid_fields(vcpu->kvm); in kvm_arch_vcpu_ioctl_run()
11529 if ((kvm_run->kvm_valid_regs & ~sync_valid_fields) || in kvm_arch_vcpu_ioctl_run()
11530 (kvm_run->kvm_dirty_regs & ~sync_valid_fields)) { in kvm_arch_vcpu_ioctl_run()
11531 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11535 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11541 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11543 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11544 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11551 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11553 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11554 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11555 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11556 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11557 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11558 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11559 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11560 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11562 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11564 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11565 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11566 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11571 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11572 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11575 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11576 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11588 if (kvm_run->kvm_valid_regs && likely(!vcpu->arch.guest_state_protected)) in kvm_arch_vcpu_ioctl_run()
11600 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11608 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11609 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11611 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11612 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11613 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11614 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11615 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11616 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11617 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11618 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11620 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11621 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11622 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11623 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11624 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11625 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11626 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11627 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11630 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11631 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11636 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_regs()
11637 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_regs()
11638 return -EINVAL; in kvm_arch_vcpu_ioctl_get_regs()
11648 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11649 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11651 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11652 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11653 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11654 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11655 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11656 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11657 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11658 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11660 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11661 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11662 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11663 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11664 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11665 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11666 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11667 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11670 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11671 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11673 vcpu->arch.exception.pending = false; in __set_regs()
11674 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11681 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_regs()
11682 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_regs()
11683 return -EINVAL; in kvm_arch_vcpu_ioctl_set_regs()
11695 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11698 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11699 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11700 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11701 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11702 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11703 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11705 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11706 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11709 sregs->idt.limit = dt.size; in __get_sregs_common()
11710 sregs->idt.base = dt.address; in __get_sregs_common()
11712 sregs->gdt.limit = dt.size; in __get_sregs_common()
11713 sregs->gdt.base = dt.address; in __get_sregs_common()
11715 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11716 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11719 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11720 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11721 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11722 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11723 sregs->apic_base = vcpu->arch.apic_base; in __get_sregs_common()
11730 if (vcpu->arch.guest_state_protected) in __get_sregs()
11733 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11734 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11735 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11744 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11749 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11750 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11757 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_sregs()
11758 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_sregs()
11759 return -EINVAL; in kvm_arch_vcpu_ioctl_get_sregs()
11783 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11784 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11785 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11786 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11788 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11802 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11806 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11829 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11830 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11831 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11834 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11835 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11836 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11838 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11850 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11862 if (ret || vcpu->mmio_needed) { in kvm_task_switch()
11863 vcpu->mmio_needed = false; in kvm_task_switch()
11864 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11865 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11866 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11870 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11871 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11878 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11881 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11884 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11886 if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11890 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11891 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11893 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11897 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11898 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11908 return -EINVAL; in __set_sregs_common()
11910 if (kvm_apic_set_base(vcpu, sregs->apic_base, true)) in __set_sregs_common()
11911 return -EINVAL; in __set_sregs_common()
11913 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11916 dt.size = sregs->idt.limit; in __set_sregs_common()
11917 dt.address = sregs->idt.base; in __set_sregs_common()
11919 dt.size = sregs->gdt.limit; in __set_sregs_common()
11920 dt.address = sregs->gdt.base; in __set_sregs_common()
11923 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11924 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11925 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11927 kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11929 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11931 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11932 kvm_x86_call(set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11934 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11935 kvm_x86_call(set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11937 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11938 kvm_x86_call(set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11941 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11946 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11949 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11950 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11951 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11952 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11953 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11954 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11956 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11957 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11963 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11965 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11986 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11999 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
12000 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
12001 !(sregs2->efer & EFER_LMA); in __set_sregs2()
12004 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
12005 return -EINVAL; in __set_sregs2()
12007 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
12008 return -EINVAL; in __set_sregs2()
12017 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
12021 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
12035 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_sregs()
12036 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_sregs()
12037 return -EINVAL; in kvm_arch_vcpu_ioctl_set_sregs()
12054 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12057 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12063 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12072 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
12073 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
12077 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12078 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
12081 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
12093 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
12094 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
12095 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
12097 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12099 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12100 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12103 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12107 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
12108 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
12118 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
12133 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
12139 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
12141 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
12142 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
12143 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
12144 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
12145 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
12155 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
12156 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_get_fpu()
12160 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
12161 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
12162 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
12163 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
12164 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
12165 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
12166 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
12167 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
12168 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
12178 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
12179 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_set_fpu()
12183 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
12185 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
12186 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
12187 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
12188 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
12189 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
12190 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
12191 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
12192 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
12202 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
12203 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
12205 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
12206 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
12208 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
12210 vcpu, &vcpu->run->s.regs.events); in store_regs()
12215 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
12216 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
12217 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
12220 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
12221 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
12224 return -EINVAL; in sync_regs()
12226 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
12229 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
12230 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
12233 return -EINVAL; in sync_regs()
12235 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
12243 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
12247 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12248 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
12250 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12251 return -EINVAL; in kvm_arch_vcpu_precreate()
12261 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
12262 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
12263 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
12265 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm); in kvm_arch_vcpu_create()
12267 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
12268 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
12270 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
12280 r = -ENOMEM; in kvm_arch_vcpu_create()
12285 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
12287 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
12289 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
12291 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
12293 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
12295 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
12302 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
12309 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_STUFF_FEATURE_MSRS)) { in kvm_arch_vcpu_create()
12310 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
12311 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
12312 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
12316 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
12317 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
12320 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
12330 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
12337 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
12339 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
12341 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
12343 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
12344 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
12345 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
12355 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
12357 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12364 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12366 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12368 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12369 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12381 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12382 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12383 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12388 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12389 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12391 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12393 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12394 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12395 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12405 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12415 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12426 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12428 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12429 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12430 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12431 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12432 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12436 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12438 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12439 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12442 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12445 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12446 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12447 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12453 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12455 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12456 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12473 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12475 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_vcpu_reset()
12477 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12478 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12486 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12497 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12504 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12526 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12596 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_enable_virtualization_cpu()
12598 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_enable_virtualization_cpu()
12600 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_enable_virtualization_cpu()
12601 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_enable_virtualization_cpu()
12631 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_enable_virtualization_cpu()
12645 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_enable_virtualization_cpu()
12647 kvm->arch.backwards_tsc_observed = true; in kvm_arch_enable_virtualization_cpu()
12649 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_enable_virtualization_cpu()
12650 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_enable_virtualization_cpu()
12660 kvm->arch.last_tsc_nsec = 0; in kvm_arch_enable_virtualization_cpu()
12661 kvm->arch.last_tsc_write = 0; in kvm_arch_enable_virtualization_cpu()
12676 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12681 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12687 kfree(kvm->arch.hv_pa_pg); in kvm_arch_free_vm()
12699 return -EINVAL; in kvm_arch_init_vm()
12701 kvm->arch.vm_type = type; in kvm_arch_init_vm()
12702 kvm->arch.has_private_mem = in kvm_arch_init_vm()
12705 kvm->arch.pre_fault_allowed = in kvm_arch_init_vm()
12718 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12719 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12722 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12723 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12725 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12727 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12728 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12729 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12730 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12732 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12734 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12736 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12737 kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT; in kvm_arch_init_vm()
12738 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12739 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12742 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12743 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12746 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12747 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12760 once_init(&kvm->arch.nx_once); in kvm_arch_init_vm()
12790 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12791 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12808 * -errno: on error
12813 * GPA->HVA translation will not change. However, the HVA is a user
12825 lockdep_assert_held(&kvm->slots_lock); in __x86_set_memory_region()
12828 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12832 if (slot && slot->npages) in __x86_set_memory_region()
12833 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12844 if (!slot || !slot->npages) in __x86_set_memory_region()
12847 old_npages = slot->npages; in __x86_set_memory_region()
12848 hva = slot->userspace_addr; in __x86_set_memory_region()
12878 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12884 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12890 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12895 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12898 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12899 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12911 vfree(slot->arch.rmap[i]); in memslot_rmap_free()
12912 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12923 vfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12924 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12932 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12939 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12942 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12943 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12945 return -ENOMEM; in memslot_rmap_alloc()
12955 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12963 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12983 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12985 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12987 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12988 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12989 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12994 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
13015 vfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
13016 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
13018 return -ENOMEM; in kvm_alloc_memslot_metadata()
13027 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
13032 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
13047 return -EINVAL; in kvm_arch_prepare_memory_region()
13050 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
13051 return -EINVAL; in kvm_arch_prepare_memory_region()
13053 if (kvm_is_gfn_alias(kvm, new->base_gfn + new->npages - 1)) in kvm_arch_prepare_memory_region()
13054 return -EINVAL; in kvm_arch_prepare_memory_region()
13060 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
13062 return -EIO; in kvm_arch_prepare_memory_region()
13075 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
13085 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
13086 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
13106 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
13115 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
13135 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
13155 * the subtly complex checks when removing write access. in kvm_mmu_slot_apply_flags()
13160 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
13167 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
13176 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
13177 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
13178 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
13179 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
13181 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
13184 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
13185 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
13188 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
13191 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
13205 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
13209 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
13225 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
13235 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_get_ip()
13254 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
13275 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
13283 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
13284 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13305 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13312 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13315 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13324 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13325 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13333 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13342 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13346 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13349 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13351 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13358 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13367 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13375 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13384 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13397 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13406 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13424 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13439 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13440 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13448 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13471 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13474 if (work->wakeup_all) in kvm_arch_async_page_present()
13475 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13477 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13478 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13480 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13482 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13483 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13487 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13488 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13494 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13508 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13515 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13521 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13528 * Non-coherent DMA assignment and de-assignment may affect whether or in kvm_noncoherent_dma_assignment_start_or_stop()
13531 * (or last) non-coherent device is (un)registered to so that new SPTEs in kvm_noncoherent_dma_assignment_start_or_stop()
13540 if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) in kvm_arch_register_noncoherent_dma()
13547 if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) in kvm_arch_unregister_noncoherent_dma()
13554 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13570 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13571 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13572 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13573 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13575 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13587 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13588 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13592 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13596 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_del_producer()
13597 prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13600 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13602 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13614 if (new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13617 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13627 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13673 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13675 u64 access = error_code & in kvm_fixup_and_inject_pf_error() local
13679 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13681 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13692 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13705 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13706 return -EIO; in kvm_handle_memory_failure()
13716 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13772 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13791 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13795 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13798 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13799 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13800 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13801 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13803 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13806 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13809 frag->data += len; in complete_sev_es_emulated_mmio()
13810 frag->gpa += len; in complete_sev_es_emulated_mmio()
13811 frag->len -= len; in complete_sev_es_emulated_mmio()
13814 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13815 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13823 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13824 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13825 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13826 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13827 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13828 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13830 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13842 return -EINVAL; in kvm_sev_es_mmio_write()
13848 bytes -= handled; in kvm_sev_es_mmio_write()
13853 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13854 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13855 frag->len = bytes; in kvm_sev_es_mmio_write()
13856 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13857 frag->data = data; in kvm_sev_es_mmio_write()
13859 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13860 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13862 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13863 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13864 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13865 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13866 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13868 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13881 return -EINVAL; in kvm_sev_es_mmio_read()
13887 bytes -= handled; in kvm_sev_es_mmio_read()
13892 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13893 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13894 frag->len = bytes; in kvm_sev_es_mmio_read()
13895 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13896 frag->data = data; in kvm_sev_es_mmio_read()
13898 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13899 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13901 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13902 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13903 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13904 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13906 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13914 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13915 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13923 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13924 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13926 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13927 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13937 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13938 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13946 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13950 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13959 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13960 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13961 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13963 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13965 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13975 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13976 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13981 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13985 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13993 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13994 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()