Lines Matching +full:smp +full:- +full:twd +full:- +full:clock

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
16 * Ben-Ami Yassour <benami@il.ibm.com>
48 #include <linux/user-return-notifier.h>
62 #include <linux/entry-kvm.h>
64 #include <linux/smp.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
146 *(((struct kvm_x86_ops *)0)->func));
149 #include <asm/kvm-x86-ops.h>
166 /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
178 * Flags to manipulate forced emulation behavior (any non-zero value will
185 int __read_mostly pi_inject_timer = -1;
445 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
457 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
549 size - useroffset, NULL); in kvm_alloc_emulator_cache()
558 vcpu->arch.apf.gfns[i] = ~0; in kvm_async_pf_hash_reset()
574 if (msrs->registered) { in kvm_on_user_return()
575 msrs->registered = false; in kvm_on_user_return()
580 values = &msrs->values[slot]; in kvm_on_user_return()
581 if (values->host != values->curr) { in kvm_on_user_return()
582 wrmsrl(kvm_uret_msrs_list[slot], values->host); in kvm_on_user_return()
583 values->curr = values->host; in kvm_on_user_return()
608 return -1; in kvm_add_user_return_msr()
623 return -1; in kvm_find_user_return_msr()
635 msrs->values[i].host = value; in kvm_user_return_msr_cpu_online()
636 msrs->values[i].curr = value; in kvm_user_return_msr_cpu_online()
645 value = (value & mask) | (msrs->values[slot].host & ~mask); in kvm_set_user_return_msr()
646 if (value == msrs->values[slot].curr) in kvm_set_user_return_msr()
652 msrs->values[slot].curr = value; in kvm_set_user_return_msr()
653 if (!msrs->registered) { in kvm_set_user_return_msr()
654 msrs->urn.on_user_return = kvm_on_user_return; in kvm_set_user_return_msr()
655 user_return_notifier_register(&msrs->urn); in kvm_set_user_return_msr()
656 msrs->registered = true; in kvm_set_user_return_msr()
666 if (msrs->registered) in drop_user_return_notifiers()
667 kvm_on_user_return(&msrs->urn); in drop_user_return_notifiers()
672 return vcpu->arch.apic_base; in kvm_get_apic_base()
684 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); in kvm_set_apic_base()
688 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) in kvm_set_apic_base()
690 if (!msr_info->host_initiated) { in kvm_set_apic_base()
697 kvm_lapic_set_base(vcpu, msr_info->data); in kvm_set_apic_base()
698 kvm_recalculate_apic_map(vcpu->kvm); in kvm_set_apic_base()
753 * #DBs can be trap-like or fault-like, the caller must check other CPU in exception_type()
772 if (!ex->has_payload) in kvm_deliver_exception_payload()
775 switch (ex->vector) { in kvm_deliver_exception_payload()
778 * "Certain debug exceptions may clear bit 0-3. The in kvm_deliver_exception_payload()
782 vcpu->arch.dr6 &= ~DR_TRAP_BITS; in kvm_deliver_exception_payload()
791 * Active low bits should be cleared if 1-setting in payload. in kvm_deliver_exception_payload()
792 * Active high bits should be set if 1-setting in payload. in kvm_deliver_exception_payload()
799 vcpu->arch.dr6 |= DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
800 vcpu->arch.dr6 |= ex->payload; in kvm_deliver_exception_payload()
801 vcpu->arch.dr6 ^= ex->payload & DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
809 vcpu->arch.dr6 &= ~BIT(12); in kvm_deliver_exception_payload()
812 vcpu->arch.cr2 = ex->payload; in kvm_deliver_exception_payload()
816 ex->has_payload = false; in kvm_deliver_exception_payload()
817 ex->payload = 0; in kvm_deliver_exception_payload()
825 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; in kvm_queue_exception_vmexit()
827 ex->vector = vector; in kvm_queue_exception_vmexit()
828 ex->injected = false; in kvm_queue_exception_vmexit()
829 ex->pending = true; in kvm_queue_exception_vmexit()
830 ex->has_error_code = has_error_code; in kvm_queue_exception_vmexit()
831 ex->error_code = error_code; in kvm_queue_exception_vmexit()
832 ex->has_payload = has_payload; in kvm_queue_exception_vmexit()
833 ex->payload = payload; in kvm_queue_exception_vmexit()
847 * morph it to a VM-Exit if L1 wants to intercept the exception. A in kvm_multiple_exception()
849 * when it was original queued, and re-checking is incorrect if _L1_ in kvm_multiple_exception()
853 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, nr, error_code)) { in kvm_multiple_exception()
859 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { in kvm_multiple_exception()
863 * On VM-Entry, an exception can be pending if and only in kvm_multiple_exception()
870 vcpu->arch.exception.injected = true; in kvm_multiple_exception()
880 vcpu->arch.exception.pending = true; in kvm_multiple_exception()
881 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
883 vcpu->arch.exception.has_error_code = has_error; in kvm_multiple_exception()
884 vcpu->arch.exception.vector = nr; in kvm_multiple_exception()
885 vcpu->arch.exception.error_code = error_code; in kvm_multiple_exception()
886 vcpu->arch.exception.has_payload = has_payload; in kvm_multiple_exception()
887 vcpu->arch.exception.payload = payload; in kvm_multiple_exception()
890 &vcpu->arch.exception); in kvm_multiple_exception()
895 prev_nr = vcpu->arch.exception.vector; in kvm_multiple_exception()
897 /* triple fault -> shutdown */ in kvm_multiple_exception()
909 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
910 vcpu->arch.exception.pending = false; in kvm_multiple_exception()
915 that instruction re-execution will regenerate lost in kvm_multiple_exception()
971 ++vcpu->stat.pf_guest; in kvm_inject_page_fault()
974 * Async #PF in L2 is always forwarded to L1 as a VM-Exit regardless of in kvm_inject_page_fault()
977 if (is_guest_mode(vcpu) && fault->async_page_fault) in kvm_inject_page_fault()
979 true, fault->error_code, in kvm_inject_page_fault()
980 true, fault->address); in kvm_inject_page_fault()
982 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, in kvm_inject_page_fault()
983 fault->address); in kvm_inject_page_fault()
990 WARN_ON_ONCE(fault->vector != PF_VECTOR); in kvm_inject_emulated_page_fault()
992 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : in kvm_inject_emulated_page_fault()
993 vcpu->arch.walk_mmu; in kvm_inject_emulated_page_fault()
999 if ((fault->error_code & PFERR_PRESENT_MASK) && in kvm_inject_emulated_page_fault()
1000 !(fault->error_code & PFERR_RSVD_MASK)) in kvm_inject_emulated_page_fault()
1001 kvm_mmu_invalidate_addr(vcpu, fault_mmu, fault->address, in kvm_inject_emulated_page_fault()
1004 fault_mmu->inject_page_fault(vcpu, fault); in kvm_inject_emulated_page_fault()
1010 atomic_inc(&vcpu->arch.nmi_queued); in kvm_inject_nmi()
1050 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); in pdptr_rsvd_bits()
1058 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in load_pdptrs()
1063 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; in load_pdptrs()
1091 if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) in load_pdptrs()
1092 kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); in load_pdptrs()
1094 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); in load_pdptrs()
1097 vcpu->arch.pdptrs_from_userspace = false; in load_pdptrs()
1122 * CR0.WP is incorporated into the MMU role, but only for non-nested, in kvm_post_set_cr0()
1169 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && in kvm_set_cr0()
1180 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && in kvm_set_cr0()
1205 if (vcpu->arch.guest_state_protected) in kvm_load_guest_xsave_state()
1210 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_guest_xsave_state()
1211 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); in kvm_load_guest_xsave_state()
1214 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_guest_xsave_state()
1215 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); in kvm_load_guest_xsave_state()
1219 vcpu->arch.pkru != vcpu->arch.host_pkru && in kvm_load_guest_xsave_state()
1220 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_guest_xsave_state()
1222 write_pkru(vcpu->arch.pkru); in kvm_load_guest_xsave_state()
1228 if (vcpu->arch.guest_state_protected) in kvm_load_host_xsave_state()
1232 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_host_xsave_state()
1234 vcpu->arch.pkru = rdpkru(); in kvm_load_host_xsave_state()
1235 if (vcpu->arch.pkru != vcpu->arch.host_pkru) in kvm_load_host_xsave_state()
1236 write_pkru(vcpu->arch.host_pkru); in kvm_load_host_xsave_state()
1241 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_host_xsave_state()
1245 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_host_xsave_state()
1255 return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; in kvm_guest_supported_xfd()
1262 u64 old_xcr0 = vcpu->arch.xcr0; in __kvm_set_xcr()
1278 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; in __kvm_set_xcr()
1297 vcpu->arch.xcr0 = xcr0; in __kvm_set_xcr()
1322 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) in __kvm_is_valid_cr4()
1341 * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB in kvm_post_set_cr4()
1355 * - CR4.PCIDE is changed from 1 to 0 in kvm_post_set_cr4()
1356 * - CR4.PGE is toggled in kvm_post_set_cr4()
1367 * - CR4.SMEP is changed from 0 to 1 in kvm_post_set_cr4()
1368 * - CR4.PAE is toggled in kvm_post_set_cr4()
1410 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_invalidate_pcid()
1445 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) in kvm_invalidate_pcid()
1448 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); in kvm_invalidate_pcid()
1481 vcpu->arch.cr3 = cr3; in kvm_set_cr3()
1490 * and it's impossible to use a non-zero PCID when PCID is disabled, in kvm_set_cr3()
1507 vcpu->arch.cr8 = cr8; in kvm_set_cr8()
1517 return vcpu->arch.cr8; in kvm_get_cr8()
1525 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { in kvm_update_dr0123()
1527 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_update_dr0123()
1535 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) in kvm_update_dr7()
1536 dr7 = vcpu->arch.guest_debug_dr7; in kvm_update_dr7()
1538 dr7 = vcpu->arch.dr7; in kvm_update_dr7()
1540 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1542 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1560 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_set_dr()
1564 vcpu->arch.db[array_index_nospec(dr, size)] = val; in kvm_set_dr()
1565 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) in kvm_set_dr()
1566 vcpu->arch.eff_db[dr] = val; in kvm_set_dr()
1572 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); in kvm_set_dr()
1578 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; in kvm_set_dr()
1589 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_get_dr()
1593 return vcpu->arch.db[array_index_nospec(dr, size)]; in kvm_get_dr()
1596 return vcpu->arch.dr6; in kvm_get_dr()
1599 return vcpu->arch.dr7; in kvm_get_dr()
1623 * 10 - MISC_PACKAGE_CTRLS
1624 * 11 - ENERGY_FILTERING_CTL
1625 * 12 - DOITM
1626 * 18 - FB_CLEAR_CTRL
1627 * 21 - XAPIC_DISABLE_STATUS
1628 * 23 - OVERCLOCKING_STATUS
1756 u64 old_efer = vcpu->arch.efer; in set_efer()
1757 u64 efer = msr_info->data; in set_efer()
1763 if (!msr_info->host_initiated) { in set_efer()
1768 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1773 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1801 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1810 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1812 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1818 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1819 ranges = msr_filter->ranges; in kvm_msr_allowed()
1821 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1828 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1834 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1843 * Returns 0 on success, non-0 otherwise.
1864 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1865 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1866 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1868 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1870 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1872 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1891 * clear the bits. This ensures cross-vendor migration will in __kvm_set_msr()
1924 * Returns 0 on success, non-0 otherwise.
1991 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1992 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1993 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
1999 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
2010 return kvm_x86_call(complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
2039 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2042 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2043 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2044 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2045 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2046 vcpu->run->msr.index = index; in kvm_msr_user_space()
2047 vcpu->run->msr.data = data; in kvm_msr_user_space()
2048 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2064 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2065 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2125 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) && in kvm_emulate_monitor_mwait()
2147 return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || in kvm_vcpu_exit_request()
2153 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2160 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2167 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2241 return -EINVAL; in do_set_msr()
2260 struct pvclock_clock clock; /* extract of a clocksource struct */ member
2273 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2276 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2277 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2278 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2279 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2280 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2281 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2282 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2284 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2285 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2286 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2287 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2288 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2289 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2290 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2292 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2294 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2296 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2301 /* Count up from boot time, but with the frequency of the raw clock. */ in get_kvmclock_base_ns()
2307 /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ in get_kvmclock_base_ns()
2356 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2358 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2359 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2362 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2365 vcpu->arch.time = system_time; in kvm_write_system_time()
2370 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2373 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2396 shift--; in kvm_get_time_scale()
2441 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2442 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2446 return -1; in set_tsc_khz()
2450 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2455 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2457 return -1; in set_tsc_khz()
2473 return -1; in kvm_set_tsc_khz()
2478 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2479 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2480 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2488 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2500 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2501 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2502 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2503 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2517 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2525 bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2526 atomic_read(&vcpu->kvm->online_vcpus)) && in kvm_track_tsc_matching()
2527 gtod_is_based_on_tsc(gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2535 if ((ka->use_master_clock && new_generation) || in kvm_track_tsc_matching()
2536 (ka->use_master_clock != use_master_clock)) in kvm_track_tsc_matching()
2539 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, in kvm_track_tsc_matching()
2540 atomic_read(&vcpu->kvm->online_vcpus), in kvm_track_tsc_matching()
2541 ka->use_master_clock, gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2548 * The most significant 64-N bits (mult) of ratio represent the
2551 * point number (mult + frac * 2^(-N)).
2574 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); in kvm_compute_l1_tsc_offset()
2576 return target_tsc - tsc; in kvm_compute_l1_tsc_offset()
2581 return vcpu->arch.l1_tsc_offset + in kvm_read_l1_tsc()
2582 kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); in kvm_read_l1_tsc()
2613 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in kvm_vcpu_write_tsc_offset()
2614 vcpu->arch.l1_tsc_offset, in kvm_vcpu_write_tsc_offset()
2617 vcpu->arch.l1_tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2625 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( in kvm_vcpu_write_tsc_offset()
2630 vcpu->arch.tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2637 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2641 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( in kvm_vcpu_write_tsc_multiplier()
2645 vcpu->arch.tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2655 * TSC is marked unstable when we're running on Hyper-V, in kvm_check_tsc_unstable()
2658 if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK) in kvm_check_tsc_unstable()
2672 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2674 lockdep_assert_held(&kvm->arch.tsc_write_lock); in __kvm_synchronize_tsc()
2680 kvm->arch.last_tsc_nsec = ns; in __kvm_synchronize_tsc()
2681 kvm->arch.last_tsc_write = tsc; in __kvm_synchronize_tsc()
2682 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; in __kvm_synchronize_tsc()
2683 kvm->arch.last_tsc_offset = offset; in __kvm_synchronize_tsc()
2685 vcpu->arch.last_guest_tsc = tsc; in __kvm_synchronize_tsc()
2697 * These values are tracked in kvm->arch.cur_xxx variables. in __kvm_synchronize_tsc()
2699 kvm->arch.cur_tsc_generation++; in __kvm_synchronize_tsc()
2700 kvm->arch.cur_tsc_nsec = ns; in __kvm_synchronize_tsc()
2701 kvm->arch.cur_tsc_write = tsc; in __kvm_synchronize_tsc()
2702 kvm->arch.cur_tsc_offset = offset; in __kvm_synchronize_tsc()
2703 kvm->arch.nr_vcpus_matched_tsc = 0; in __kvm_synchronize_tsc()
2704 } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { in __kvm_synchronize_tsc()
2705 kvm->arch.nr_vcpus_matched_tsc++; in __kvm_synchronize_tsc()
2709 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; in __kvm_synchronize_tsc()
2710 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; in __kvm_synchronize_tsc()
2711 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; in __kvm_synchronize_tsc()
2719 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2725 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2728 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2730 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2737 } else if (kvm->arch.user_set_tsc) { in kvm_synchronize_tsc()
2738 u64 tsc_exp = kvm->arch.last_tsc_write + in kvm_synchronize_tsc()
2740 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; in kvm_synchronize_tsc()
2742 * Here lies UAPI baggage: when a user-initiated TSC write has in kvm_synchronize_tsc()
2753 * come from the kernel's default vCPU creation. Make the 1-second in kvm_synchronize_tsc()
2762 kvm->arch.user_set_tsc = true; in kvm_synchronize_tsc()
2771 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2773 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2783 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2789 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2795 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2798 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2807 u64 last = pvclock_gtod_data.clock.cycle_last; in read_tsc()
2824 static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, in vgettsc() argument
2830 switch (clock->vclock_mode) { in vgettsc()
2836 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2837 clock->mask; in vgettsc()
2846 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2847 clock->mask; in vgettsc()
2856 return v * clock->mult; in vgettsc()
2861 * frequency of CLOCK_MONOTONIC_RAW (hence adding gtos->offs_boot).
2871 seq = read_seqcount_begin(&gtod->seq); in do_kvmclock_base()
2872 ns = gtod->raw_clock.base_cycles; in do_kvmclock_base()
2873 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode); in do_kvmclock_base()
2874 ns >>= gtod->raw_clock.shift; in do_kvmclock_base()
2875 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_kvmclock_base()
2876 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_kvmclock_base()
2894 seq = read_seqcount_begin(&gtod->seq); in do_monotonic()
2895 ns = gtod->clock.base_cycles; in do_monotonic()
2896 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_monotonic()
2897 ns >>= gtod->clock.shift; in do_monotonic()
2898 ns += ktime_to_ns(gtod->clock.offset); in do_monotonic()
2899 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_monotonic()
2913 seq = read_seqcount_begin(&gtod->seq); in do_realtime()
2914 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2915 ns = gtod->clock.base_cycles; in do_realtime()
2916 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_realtime()
2917 ns >>= gtod->clock.shift; in do_realtime()
2918 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_realtime()
2920 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2921 ts->tv_nsec = ns; in do_realtime()
2934 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) in kvm_get_time_and_clockread()
2948 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) in kvm_get_monotonic_and_clockread()
2966 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) in kvm_get_walltime_and_clockread()
2989 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2990 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2991 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2995 * - ret0 < ret1
2996 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2998 * - 0 < N - M => M < N
3017 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
3021 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
3022 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
3023 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
3026 * If the host uses TSC clock, then passthrough TSC as stable in pvclock_update_vm_gtod_copy()
3030 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
3031 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
3033 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
3034 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
3035 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
3037 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
3040 vclock_mode = pvclock_gtod_data.clock.vclock_mode; in pvclock_update_vm_gtod_copy()
3041 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
3053 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
3054 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3067 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3071 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3072 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3091 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3105 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3108 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3114 data->flags = 0; in __get_kvmclock()
3115 if (ka->use_master_clock && in __get_kvmclock()
3120 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3121 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3122 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3125 data->host_tsc = rdtsc(); in __get_kvmclock()
3127 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3128 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3129 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3133 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3135 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3143 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3147 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3149 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3157 return data.clock; in get_kvmclock_ns()
3165 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_setup_guest_pvclock()
3169 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3171 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3176 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3179 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3188 guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3192 vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3194 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_setup_guest_pvclock()
3195 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; in kvm_setup_guest_pvclock()
3196 vcpu->pvclock_set_guest_stopped_request = false; in kvm_setup_guest_pvclock()
3199 memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); in kvm_setup_guest_pvclock()
3202 guest_hv_clock->flags &= ~PVCLOCK_TSC_STABLE_BIT; in kvm_setup_guest_pvclock()
3206 guest_hv_clock->version = ++vcpu->hv_clock.version; in kvm_setup_guest_pvclock()
3209 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3211 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); in kvm_setup_guest_pvclock()
3218 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3219 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3232 ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE; in kvm_guest_time_update()
3239 * If the host uses TSC clock, then passthrough TSC as stable in kvm_guest_time_update()
3243 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3244 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3246 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3247 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3249 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3251 /* Keep irq disabled to prevent changes to the clock */ in kvm_guest_time_update()
3267 * We may have to catch up the TSC to match elapsed wall clock in kvm_guest_time_update()
3276 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3279 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3290 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3292 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3294 &vcpu->hv_clock.tsc_shift, in kvm_guest_time_update()
3295 &vcpu->hv_clock.tsc_to_system_mul); in kvm_guest_time_update()
3296 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3300 vcpu->hv_clock.tsc_timestamp = tsc_timestamp; in kvm_guest_time_update()
3301 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3302 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3309 vcpu->hv_clock.flags = pvclock_flags; in kvm_guest_time_update()
3311 if (vcpu->pv_time.active) in kvm_guest_time_update()
3312 kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0, false); in kvm_guest_time_update()
3314 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3315 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3318 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3319 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0, in kvm_guest_time_update()
3322 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); in kvm_guest_time_update()
3327 * The pvclock_wall_clock ABI tells the guest the wall clock time at
3330 * In fact those clocks are subtly different; wall clock frequency is
3349 struct kvm_arch *ka = &kvm->arch; in kvm_get_wall_clock_epoch()
3355 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_get_wall_clock_epoch()
3358 if (!ka->use_master_clock) in kvm_get_wall_clock_epoch()
3380 hv_clock.tsc_timestamp = ka->master_cycle_now; in kvm_get_wall_clock_epoch()
3381 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in kvm_get_wall_clock_epoch()
3383 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_get_wall_clock_epoch()
3387 * successful, calculate the KVM clock at the corresponding time and in kvm_get_wall_clock_epoch()
3389 * since 1970-01-01. in kvm_get_wall_clock_epoch()
3395 return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - in kvm_get_wall_clock_epoch()
3399 return ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_get_wall_clock_epoch()
3404 * vcpu->cpu migration, should not allow system_timestamp from
3410 * We need to rate-limit these requests though, as they can
3413 * by the delay we use to rate-limit the updates.
3435 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3438 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3451 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3452 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3473 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3480 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3482 u32 msr = msr_info->index; in set_msr_mce()
3483 u64 data = msr_info->data; in set_msr_mce()
3488 vcpu->arch.mcg_status = data; in set_msr_mce()
3492 (data || !msr_info->host_initiated)) in set_msr_mce()
3496 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3498 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3499 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3503 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3508 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3509 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3510 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3512 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3513 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3525 * single-bit ECC data errors. in set_msr_mce()
3533 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3536 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3540 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3541 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3542 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3554 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3576 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3584 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3588 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3589 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3598 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3605 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3607 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3614 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3615 vcpu->arch.time = 0; in kvmclock_reset()
3620 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3629 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3645 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3654 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3662 * prior before nested VM-Enter/VM-Exit.
3676 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3679 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3683 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3688 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3691 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3694 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3696 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3697 gpa != ghc->gpa || in record_steal_time()
3698 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3700 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3702 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3703 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3707 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3714 int err = -EFAULT; in record_steal_time()
3725 "+m" (st->preempted)); in record_steal_time()
3731 vcpu->arch.st.preempted = 0; in record_steal_time()
3733 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3744 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3745 vcpu->arch.st.preempted = 0; in record_steal_time()
3748 unsafe_get_user(version, &st->version, out); in record_steal_time()
3753 unsafe_put_user(version, &st->version, out); in record_steal_time()
3757 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3758 steal += current->sched_info.run_delay - in record_steal_time()
3759 vcpu->arch.st.last_steal; in record_steal_time()
3760 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3761 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3764 unsafe_put_user(version, &st->version, out); in record_steal_time()
3769 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3774 u32 msr = msr_info->index; in kvm_set_msr_common()
3775 u64 data = msr_info->data; in kvm_set_msr_common()
3777 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) in kvm_set_msr_common()
3792 if (msr_info->host_initiated) in kvm_set_msr_common()
3793 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3796 if (!msr_info->host_initiated) in kvm_set_msr_common()
3798 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3801 if (!msr_info->host_initiated) in kvm_set_msr_common()
3811 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3814 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3820 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3848 if (!msr_info->host_initiated && in kvm_set_msr_common()
3875 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3887 vcpu->arch.pat = data; in kvm_set_msr_common()
3901 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3902 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3909 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3913 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3915 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3925 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3929 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3932 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3937 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3939 vcpu->arch.smbase = data; in kvm_set_msr_common()
3942 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3945 if (msr_info->host_initiated) { in kvm_set_msr_common()
3948 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3950 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3954 if (!msr_info->host_initiated && in kvm_set_msr_common()
3964 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3968 if (!msr_info->host_initiated) in kvm_set_msr_common()
3970 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3976 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3977 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3983 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3984 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3990 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
3996 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
4016 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
4030 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
4051 if (data & (-1ULL << 1)) in kvm_set_msr_common()
4054 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
4059 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4060 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4077 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
4095 msr_info->host_initiated); in kvm_set_msr_common()
4098 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
4106 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
4111 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
4114 if (!msr_info->host_initiated || in kvm_set_msr_common()
4118 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
4125 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
4129 if (!msr_info->host_initiated && in kvm_set_msr_common()
4136 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
4139 if (!msr_info->host_initiated && in kvm_set_msr_common()
4146 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
4162 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4172 data = vcpu->arch.mcg_cap; in get_msr_mce()
4177 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4180 data = vcpu->arch.mcg_status; in get_msr_mce()
4182 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4183 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4189 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4190 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4191 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4193 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4194 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4198 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4199 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4200 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4211 switch (msr_info->index) { in kvm_get_msr_common()
4234 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4241 msr_info->data = 0; in kvm_get_msr_common()
4247 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4249 msr_info->data = 0; in kvm_get_msr_common()
4252 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4255 if (!msr_info->host_initiated && in kvm_get_msr_common()
4258 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4261 if (!msr_info->host_initiated && in kvm_get_msr_common()
4264 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4267 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4276 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4281 if (msr_info->host_initiated) { in kvm_get_msr_common()
4282 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4283 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4285 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4286 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4289 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4293 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4298 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4300 msr_info->data = 3; in kvm_get_msr_common()
4307 * and 266MHz for model 3, or 4. Set Core Clock in kvm_get_msr_common()
4314 msr_info->data = 1 << 24; in kvm_get_msr_common()
4317 msr_info->data = kvm_get_apic_base(vcpu); in kvm_get_msr_common()
4320 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4322 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4325 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4328 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4331 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4333 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4336 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4340 msr_info->data = 1000ULL; in kvm_get_msr_common()
4342 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4345 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4351 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4357 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4363 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4369 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4375 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4381 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4387 msr_info->data = 0; in kvm_get_msr_common()
4393 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4399 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4405 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4412 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4413 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4414 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4415 msr_info->host_initiated); in kvm_get_msr_common()
4417 if (!msr_info->host_initiated && in kvm_get_msr_common()
4420 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4424 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4432 msr_info->data = 0x20000000; in kvm_get_msr_common()
4446 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4447 msr_info->host_initiated); in kvm_get_msr_common()
4460 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4465 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4470 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4473 if (!msr_info->host_initiated && in kvm_get_msr_common()
4474 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4476 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4479 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4482 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4486 if (!msr_info->host_initiated && in kvm_get_msr_common()
4490 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4493 if (!msr_info->host_initiated && in kvm_get_msr_common()
4497 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4501 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4522 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4544 r = -EFAULT; in msr_io()
4548 r = -E2BIG; in msr_io()
4553 entries = memdup_user(user_msrs->entries, size); in msr_io()
4561 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4562 r = -EFAULT; in msr_io()
4583 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4587 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4591 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4768 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4769 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4776 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4821 if (attr->group) { in __kvm_x86_dev_get_attr()
4823 return kvm_x86_call(dev_get_attr)(attr->group, attr->attr, val); in __kvm_x86_dev_get_attr()
4824 return -ENXIO; in __kvm_x86_dev_get_attr()
4827 switch (attr->attr) { in __kvm_x86_dev_get_attr()
4832 return -ENXIO; in __kvm_x86_dev_get_attr()
4838 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_x86_dev_get_attr()
4847 return -EFAULT; in kvm_x86_dev_get_attr()
4871 r = -EFAULT; in kvm_arch_dev_ioctl()
4878 r = -E2BIG; in kvm_arch_dev_ioctl()
4881 r = -EFAULT; in kvm_arch_dev_ioctl()
4882 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4885 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4897 r = -EFAULT; in kvm_arch_dev_ioctl()
4901 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4906 r = -EFAULT; in kvm_arch_dev_ioctl()
4913 r = -EFAULT; in kvm_arch_dev_ioctl()
4924 r = -EFAULT; in kvm_arch_dev_ioctl()
4931 r = -E2BIG; in kvm_arch_dev_ioctl()
4934 r = -EFAULT; in kvm_arch_dev_ioctl()
4935 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4951 r = -EFAULT; in kvm_arch_dev_ioctl()
4959 r = -EFAULT; in kvm_arch_dev_ioctl()
4966 r = -EINVAL; in kvm_arch_dev_ioctl()
4980 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4987 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_vcpu_load()
4989 if (vcpu->scheduled_out && pmu->version && pmu->event_count) { in kvm_arch_vcpu_load()
4990 pmu->need_cleanup = true; in kvm_arch_vcpu_load()
4997 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4998 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4999 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
5006 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
5009 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
5010 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
5011 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
5015 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
5016 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
5017 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
5023 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
5025 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
5033 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
5035 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
5037 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5039 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
5047 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
5051 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
5054 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
5058 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
5060 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
5061 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
5065 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
5066 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
5069 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
5073 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
5076 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
5078 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
5079 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
5080 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
5083 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
5084 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
5086 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
5087 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
5089 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
5096 if (vcpu->preempted) { in kvm_arch_vcpu_put()
5097 vcpu->arch.preempted_in_kernel = kvm_arch_vcpu_in_kernel(vcpu); in kvm_arch_vcpu_put()
5103 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
5104 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
5108 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
5112 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
5159 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
5170 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
5171 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
5173 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
5174 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5180 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5181 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5183 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5184 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5186 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5187 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5189 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5204 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5205 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5206 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5216 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5222 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5225 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5228 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5230 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5243 * - none of the bits for Machine Check Exceptions are set
5244 * - both the VAL (valid) and UC (uncorrectable) bits are set
5245 * MCI_STATUS_PCC - Processor Context Corrupted
5246 * MCI_STATUS_S - Signaled as a Machine Check Exception
5247 * MCI_STATUS_AR - Software recoverable Action Required
5251 return !mce->mcg_status && in is_ucna()
5252 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5253 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5254 (mce->status & MCI_STATUS_UC); in is_ucna()
5259 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5261 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5262 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5263 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5264 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5267 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5271 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5279 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5281 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5283 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5284 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5286 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5295 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5296 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5302 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5304 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5305 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5311 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5312 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5313 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5314 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5315 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5320 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5321 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5322 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5323 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5344 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5345 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5348 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5349 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5350 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5351 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5353 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5358 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5363 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5364 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5375 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5376 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5377 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5383 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5384 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5386 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5387 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5388 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5389 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5390 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5392 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5393 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5394 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5395 events->interrupt.shadow = kvm_x86_call(get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5397 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5398 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5399 events->nmi.masked = kvm_x86_call(get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5401 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5404 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5405 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5406 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5407 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5409 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5411 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5414 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5415 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5416 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5417 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5418 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5425 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5431 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5433 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5434 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5435 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5436 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5437 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5439 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5441 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5442 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5445 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5446 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5447 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5450 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5451 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5452 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5453 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5459 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5460 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5463 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5465 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5467 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5469 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5470 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5471 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5472 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5473 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5474 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5475 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5477 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5478 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5479 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5480 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5482 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5484 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5485 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5486 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5487 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5488 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5491 kvm_x86_call(set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5493 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5495 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5497 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5499 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5501 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5504 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5506 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5507 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5508 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5510 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5514 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5515 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5516 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5520 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5521 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5523 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5527 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5528 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5529 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5530 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5546 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_debugregs()
5547 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_debugregs()
5548 return -EINVAL; in kvm_vcpu_ioctl_x86_get_debugregs()
5552 BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.db) != ARRAY_SIZE(dbgregs->db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5553 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_get_debugregs()
5554 dbgregs->db[i] = vcpu->arch.db[i]; in kvm_vcpu_ioctl_x86_get_debugregs()
5556 dbgregs->dr6 = vcpu->arch.dr6; in kvm_vcpu_ioctl_x86_get_debugregs()
5557 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5566 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_debugregs()
5567 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_debugregs()
5568 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5570 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5571 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5573 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5574 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5575 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5576 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5578 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_set_debugregs()
5579 vcpu->arch.db[i] = dbgregs->db[i]; in kvm_vcpu_ioctl_x86_set_debugregs()
5582 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5583 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5605 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5608 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5609 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_get_xsave2()
5611 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5612 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5619 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5620 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5626 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5627 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_set_xsave()
5629 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5630 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5632 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5638 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_xcrs()
5639 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_xcrs()
5640 return -EINVAL; in kvm_vcpu_ioctl_x86_get_xcrs()
5643 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5647 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5648 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5649 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5650 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5659 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_xcrs()
5660 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_xcrs()
5661 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5664 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5666 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5667 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5669 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5671 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5673 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5677 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5689 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5690 return -EINVAL; in kvm_set_guest_paused()
5691 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5701 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5706 r = -ENXIO; in kvm_arch_tsc_has_attr()
5715 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_get_attr()
5718 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5720 r = -EFAULT; in kvm_arch_tsc_get_attr()
5721 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5726 r = -ENXIO; in kvm_arch_tsc_get_attr()
5735 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_set_attr()
5736 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5739 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5745 r = -EFAULT; in kvm_arch_tsc_set_attr()
5749 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5751 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5752 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5753 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5755 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5758 kvm->arch.user_set_tsc = true; in kvm_arch_tsc_set_attr()
5760 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5766 r = -ENXIO; in kvm_arch_tsc_set_attr()
5780 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5783 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5803 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5804 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5806 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5809 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5810 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5814 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5815 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5816 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5824 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5825 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5826 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5828 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5831 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5837 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5842 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5846 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5847 if (vcpu->arch.pv_cpuid.enforce) in kvm_vcpu_ioctl_enable_cap()
5852 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5859 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5875 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5880 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5886 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5893 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5908 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5926 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5929 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5936 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5940 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5947 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5951 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5954 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5961 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5963 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5967 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5969 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5975 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5981 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5991 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5994 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5997 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5999 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6005 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6014 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6025 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6034 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6050 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6060 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6069 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6070 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
6074 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6082 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6089 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6102 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6105 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6113 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6123 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6131 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6151 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6167 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
6177 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6187 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6188 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
6191 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
6192 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6193 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6196 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
6202 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6203 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6205 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6217 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6218 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6221 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6225 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6240 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6241 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6242 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6254 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6259 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6265 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6273 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6274 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6275 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6279 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6283 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6290 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6291 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6292 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6310 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6328 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6329 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6344 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6346 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6349 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6351 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6357 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6361 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6363 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6367 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6371 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6374 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6382 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6386 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6388 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6389 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6391 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6394 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6395 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6397 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6400 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6403 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6412 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6414 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6416 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6417 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6418 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6425 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6427 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6428 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6430 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6431 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6437 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6438 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6439 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6440 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6441 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6442 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6451 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6453 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6454 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6455 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6458 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6459 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6460 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6462 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6464 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6471 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6473 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6477 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6478 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6479 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6490 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6491 * VM-Exit. in kvm_arch_sync_dirty_log()
6507 return -ENXIO; in kvm_vm_ioctl_irq_line()
6509 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6510 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6520 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6521 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6523 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6525 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6526 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6530 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6534 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6535 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6536 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6538 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6541 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6545 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6546 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6550 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6554 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6555 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6558 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6559 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6560 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6561 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6566 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6567 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) in kvm_vm_ioctl_enable_cap()
6570 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6571 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6573 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6578 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6581 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && in kvm_vm_ioctl_enable_cap()
6583 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6584 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6585 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6586 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6587 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6593 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6597 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6601 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6605 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6606 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6608 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6612 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6613 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6616 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6617 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6621 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6622 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6629 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6636 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6638 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6643 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6647 r = kvm_x86_call(vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6650 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6654 r = kvm_x86_call(vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6657 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6658 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6661 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6665 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6666 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6668 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6672 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6673 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6676 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6677 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6678 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6681 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6684 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6685 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6688 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6689 if (kvm->arch.bsp_vcpu_id > cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6691 } else if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6693 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6694 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6697 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6700 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6701 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6705 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6707 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6708 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6709 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6710 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6713 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6716 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6730 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6734 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6737 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6738 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6739 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6742 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6745 u64 bus_cycle_ns = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6752 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6758 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6760 r = -ENXIO; in kvm_vm_ioctl_enable_cap()
6761 else if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6762 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6764 kvm->arch.apic_bus_cycle_ns = bus_cycle_ns; in kvm_vm_ioctl_enable_cap()
6765 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6769 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6783 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6794 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6795 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6806 if (!user_range->nmsrs) in kvm_add_msr_filter()
6809 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6810 return -EINVAL; in kvm_add_msr_filter()
6812 if (!user_range->flags) in kvm_add_msr_filter()
6813 return -EINVAL; in kvm_add_msr_filter()
6815 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6817 return -EINVAL; in kvm_add_msr_filter()
6819 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6823 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6824 .flags = user_range->flags, in kvm_add_msr_filter()
6825 .base = user_range->base, in kvm_add_msr_filter()
6826 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6830 msr_filter->count++; in kvm_add_msr_filter()
6843 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6844 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6846 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6847 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6849 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6851 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6855 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6857 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6858 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6865 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6866 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6867 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6868 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6869 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6898 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6899 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6910 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6918 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6919 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6920 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6921 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6941 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6943 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6949 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6953 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6976 return -EFAULT; in kvm_vm_ioctl_get_clock()
6983 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6988 return -EFAULT; in kvm_vm_ioctl_set_clock()
6995 return -EINVAL; in kvm_vm_ioctl_set_clock()
7006 * 'system_time' when 'data.clock' is very small. in kvm_vm_ioctl_set_clock()
7015 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
7018 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
7019 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
7022 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
7029 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
7031 int r = -ENOTTY; in kvm_arch_vm_ioctl()
7033 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
7050 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7051 r = -EINVAL; in kvm_arch_vm_ioctl()
7052 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7054 r = -EFAULT; in kvm_arch_vm_ioctl()
7059 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7066 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7068 r = -EEXIST; in kvm_arch_vm_ioctl()
7072 r = -EINVAL; in kvm_arch_vm_ioctl()
7073 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7092 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
7094 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
7097 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7104 r = -EFAULT; in kvm_arch_vm_ioctl()
7109 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7110 r = -EEXIST; in kvm_arch_vm_ioctl()
7111 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7113 r = -ENOENT; in kvm_arch_vm_ioctl()
7116 r = -ENOMEM; in kvm_arch_vm_ioctl()
7117 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
7118 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7121 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7133 r = -ENXIO; in kvm_arch_vm_ioctl()
7139 r = -EFAULT; in kvm_arch_vm_ioctl()
7157 r = -ENXIO; in kvm_arch_vm_ioctl()
7166 r = -EFAULT; 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()
7195 r = -ENXIO; in kvm_arch_vm_ioctl()
7196 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7201 r = -EFAULT; in kvm_arch_vm_ioctl()
7208 r = -EFAULT; in kvm_arch_vm_ioctl()
7211 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7212 r = -ENXIO; in kvm_arch_vm_ioctl()
7213 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7217 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7222 r = -EFAULT; in kvm_arch_vm_ioctl()
7225 r = -ENXIO; in kvm_arch_vm_ioctl()
7226 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7233 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7234 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7235 r = -EBUSY; in kvm_arch_vm_ioctl()
7237 (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) in kvm_arch_vm_ioctl()
7238 r = -EINVAL; in kvm_arch_vm_ioctl()
7240 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
7241 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7246 r = -EFAULT; in kvm_arch_vm_ioctl()
7255 r = -EFAULT; in kvm_arch_vm_ioctl()
7260 r = -EFAULT; in kvm_arch_vm_ioctl()
7266 r = -EFAULT; in kvm_arch_vm_ioctl()
7275 r = -EFAULT; in kvm_arch_vm_ioctl()
7291 r = -EINVAL; in kvm_arch_vm_ioctl()
7301 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7307 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7311 r = -ENOTTY; in kvm_arch_vm_ioctl()
7321 r = -EFAULT; in kvm_arch_vm_ioctl()
7325 r = -ENOTTY; in kvm_arch_vm_ioctl()
7335 r = -EFAULT; in kvm_arch_vm_ioctl()
7339 r = -ENOTTY; in kvm_arch_vm_ioctl()
7350 r = -EFAULT; in kvm_arch_vm_ioctl()
7365 return -EFAULT; in kvm_arch_vm_ioctl()
7371 r = -ENOTTY; in kvm_arch_vm_ioctl()
7431 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7436 MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7437 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7442 MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7443 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7448 MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1: in kvm_probe_msr_to_save()
7449 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7518 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7523 len -= n; in vcpu_mmio_write()
7538 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7545 len -= n; in vcpu_mmio_read()
7567 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7572 /* NPT walks are always user-walks */ in translate_nested_gpa()
7574 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7582 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7585 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7592 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7596 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7604 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7606 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7613 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7618 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7619 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7620 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7632 bytes -= toread; in kvm_read_guest_virt_helper()
7646 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7652 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7657 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7659 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7705 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7710 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7711 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7712 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7723 bytes -= towrite; in kvm_write_guest_virt_helper()
7751 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7811 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7821 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7822 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7823 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7824 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7829 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7832 return -1; in vcpu_mmio_gva_to_gpa()
7863 if (vcpu->mmio_read_completed) { in read_prepare()
7865 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7866 vcpu->mmio_read_completed = 0; in read_prepare()
7901 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7903 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7929 bool write = ops->write; in emulator_read_write_onepage()
7931 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7940 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7941 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7942 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7950 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7956 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7961 bytes -= handled; in emulator_read_write_onepage()
7964 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7965 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7966 frag->gpa = gpa; in emulator_read_write_onepage()
7967 frag->data = val; in emulator_read_write_onepage()
7968 frag->len = bytes; in emulator_read_write_onepage()
7982 if (ops->read_write_prepare && in emulator_read_write()
7983 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7986 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7989 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7992 now = -addr & ~PAGE_MASK; in emulator_read_write()
7999 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
8002 bytes -= now; in emulator_read_write()
8010 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
8013 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
8015 vcpu->mmio_needed = 1; in emulator_read_write()
8016 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
8018 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
8019 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
8020 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
8021 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
8023 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
8063 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
8077 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
8081 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
8139 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
8155 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
8164 vcpu->arch.pio.port = port; in emulator_pio_in_out()
8165 vcpu->arch.pio.in = in; in emulator_pio_in_out()
8166 vcpu->arch.pio.count = count; in emulator_pio_in_out()
8167 vcpu->arch.pio.size = size; in emulator_pio_in_out()
8170 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
8172 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
8174 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
8175 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
8176 vcpu->run->io.size = size; in emulator_pio_in_out()
8177 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
8178 vcpu->run->io.count = count; in emulator_pio_in_out()
8179 vcpu->run->io.port = port; in emulator_pio_in_out()
8195 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
8196 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
8197 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
8198 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
8199 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
8207 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
8255 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8256 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
8259 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8293 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8306 value = vcpu->arch.cr2; in emulator_get_cr()
8335 vcpu->arch.cr2 = val; in emulator_set_cr()
8348 res = -1; in emulator_set_cr()
8409 desc->type = var.type; in emulator_get_segment()
8410 desc->s = var.s; in emulator_get_segment()
8411 desc->dpl = var.dpl; in emulator_get_segment()
8412 desc->p = var.present; in emulator_get_segment()
8413 desc->avl = var.avl; in emulator_get_segment()
8414 desc->l = var.l; in emulator_get_segment()
8415 desc->d = var.db; in emulator_get_segment()
8416 desc->g = var.g; in emulator_get_segment()
8434 if (desc->g) in emulator_set_segment()
8436 var.type = desc->type; in emulator_set_segment()
8437 var.dpl = desc->dpl; in emulator_set_segment()
8438 var.db = desc->d; in emulator_set_segment()
8439 var.s = desc->s; in emulator_set_segment()
8440 var.l = desc->l; in emulator_set_segment()
8441 var.g = desc->g; in emulator_set_segment()
8442 var.avl = desc->avl; in emulator_set_segment()
8443 var.present = desc->p; in emulator_set_segment()
8516 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8524 &ctxt->exception); in emulator_intercept()
8599 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8601 if (!kvm->vm_bugged) in emulator_vm_bugged()
8684 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8686 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8687 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8688 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8689 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8690 ctxt->exception.error_code); in inject_emulated_exception()
8692 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8705 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8706 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8707 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8714 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8719 ctxt->gpa_available = false; in init_emulate_ctxt()
8720 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8721 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8723 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8724 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8725 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8729 ctxt->interruptibility = 0; in init_emulate_ctxt()
8730 ctxt->have_exception = false; in init_emulate_ctxt()
8731 ctxt->exception.vector = -1; in init_emulate_ctxt()
8732 ctxt->perm_ok = false; in init_emulate_ctxt()
8735 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8740 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8745 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8746 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8747 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8753 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8754 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8755 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8763 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8776 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8777 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8789 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8792 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8793 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8795 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8797 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8798 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8799 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8800 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8803 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8804 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8807 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8812 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8814 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8815 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8833 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8835 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8843 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8872 * a SPTE and write-protect the gfn to resolve the !PRESENT fault, and in kvm_unprotect_and_retry_on_failure()
8881 * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the in kvm_unprotect_and_retry_on_failure()
8882 * guest to let the CPU re-execute the instruction in the hope that the in kvm_unprotect_and_retry_on_failure()
8917 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8919 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8920 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8921 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8922 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8923 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8993 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8994 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8995 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
8998 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
8999 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
9002 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
9003 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
9004 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
9005 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
9011 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
9015 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
9016 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
9030 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
9032 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9049 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9062 * (and wrong) when emulating on an intercepted fault-like exception[*], as
9072 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
9080 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
9090 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
9107 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
9114 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
9132 if (ctxt->have_exception && in x86_emulate_instruction()
9135 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
9138 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
9139 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
9157 * injecting single-step #DBs. in x86_emulate_instruction()
9160 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
9161 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
9163 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
9170 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9171 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
9172 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
9177 * If emulation was caused by a write-protection #PF on a non-page_table in x86_emulate_instruction()
9189 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
9190 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
9197 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
9200 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
9201 ctxt->gpa_available = true; in x86_emulate_instruction()
9202 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
9206 ctxt->exception.address = 0; in x86_emulate_instruction()
9222 if (ctxt->have_exception) { in x86_emulate_instruction()
9223 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9224 vcpu->mmio_needed = false; in x86_emulate_instruction()
9227 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9228 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9229 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9230 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9233 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9236 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9237 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9239 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9242 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9243 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9254 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9255 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9258 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9260 * of which are fault-like. in x86_emulate_instruction()
9262 if (!ctxt->have_exception || in x86_emulate_instruction()
9263 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9265 if (ctxt->is_branch) in x86_emulate_instruction()
9267 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9268 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9271 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9280 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9283 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9303 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9309 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9311 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9331 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9332 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9336 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9337 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9347 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9349 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9350 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9355 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9378 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9379 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9410 khz = freq->new; in tsc_khz_changed()
9431 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9499 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9502 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9508 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9531 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9533 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9536 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9564 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9565 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9619 * Disable master clock if host does not trust, or does not use, in pvclock_gtod_notify()
9623 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9636 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9646 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9649 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9667 return -EIO; in kvm_x86_check_processor_compatibility()
9686 return -EEXIST; in kvm_x86_vendor_init()
9696 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9701 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9714 return -EIO; in kvm_x86_vendor_init()
9722 return -ENOMEM; in kvm_x86_vendor_init()
9728 r = -ENOMEM; in kvm_x86_vendor_init()
9750 kvm_init_pmu_capability(ops->pmu_ops); in kvm_x86_vendor_init()
9755 r = ops->hardware_setup(); in kvm_x86_vendor_init()
9774 if (pi_inject_timer == -1) in kvm_x86_vendor_init()
9783 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in kvm_x86_vendor_init()
9867 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9873 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9874 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9877 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9886 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9888 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9897 * @apicid - apicid of vcpu to be kicked.
9917 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9923 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9951 set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason, true); in kvm_apicv_init()
9953 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9961 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9967 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9969 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9970 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9974 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9984 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
9992 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
9997 ++vcpu->stat.hypercalls; in complete_hypercall_exit()
10019 ret = -KVM_EPERM; in __kvm_emulate_hypercall()
10023 ret = -KVM_ENOSYS; in __kvm_emulate_hypercall()
10033 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in __kvm_emulate_hypercall()
10046 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in __kvm_emulate_hypercall()
10058 ret = -KVM_ENOSYS; in __kvm_emulate_hypercall()
10059 if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) in __kvm_emulate_hypercall()
10064 ret = -KVM_EINVAL; in __kvm_emulate_hypercall()
10068 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in __kvm_emulate_hypercall()
10069 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in __kvm_emulate_hypercall()
10070 vcpu->run->hypercall.args[0] = gpa; in __kvm_emulate_hypercall()
10071 vcpu->run->hypercall.args[1] = npages; in __kvm_emulate_hypercall()
10072 vcpu->run->hypercall.args[2] = attrs; in __kvm_emulate_hypercall()
10073 vcpu->run->hypercall.flags = 0; in __kvm_emulate_hypercall()
10075 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in __kvm_emulate_hypercall()
10077 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in __kvm_emulate_hypercall()
10078 vcpu->arch.complete_userspace_io = complete_hypercall_exit; in __kvm_emulate_hypercall()
10083 ret = -KVM_ENOSYS; in __kvm_emulate_hypercall()
10088 ++vcpu->stat.hypercalls; in __kvm_emulate_hypercall()
10099 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
10136 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
10137 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
10138 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
10139 ctxt->have_exception = true; in emulator_fix_hypercall()
10146 &ctxt->exception); in emulator_fix_hypercall()
10151 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
10152 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
10155 /* Called within kvm->srcu read side. */
10158 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
10160 kvm_run->if_flag = kvm_x86_call(get_if_flag)(vcpu); in post_kvm_run_save()
10161 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
10162 kvm_run->apic_base = kvm_get_apic_base(vcpu); in post_kvm_run_save()
10164 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
10165 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
10169 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
10171 kvm_run->flags |= KVM_RUN_X86_GUEST_MODE; in post_kvm_run_save()
10184 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10187 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10190 max_irr = -1; in update_cr8_intercept()
10192 if (max_irr != -1) in update_cr8_intercept()
10204 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10208 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10217 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10220 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10222 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10223 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10224 vcpu->arch.exception.error_code, in kvm_inject_exception()
10225 vcpu->arch.exception.injected); in kvm_inject_exception()
10235 * injected as part of a previous VM-Enter, but weren't successfully delivered
10236 * and need to be re-injected.
10241 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10242 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10247 * instruction boundaries for asynchronous events. However, because VM-Exits
10253 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10276 * Process nested events first, as nested VM-Exit supersedes event in kvm_check_and_inject_events()
10277 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10278 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10286 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10290 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10299 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10301 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10304 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10308 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10312 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10314 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10318 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10319 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10322 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10323 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10327 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10330 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10331 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10338 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10339 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10340 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10342 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10343 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10347 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10352 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10354 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10355 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10356 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10361 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10363 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10367 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10368 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10369 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10370 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10377 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10378 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10384 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10389 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10390 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10396 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10399 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10401 -EBUSY; in kvm_check_and_inject_events()
10405 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10406 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10414 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10416 -EBUSY; in kvm_check_and_inject_events()
10420 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10421 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10426 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10432 -EBUSY; in kvm_check_and_inject_events()
10438 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10449 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10450 kvm_x86_ops.nested_ops->has_events(vcpu, true)) in kvm_check_and_inject_events()
10455 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10457 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10465 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10466 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10470 if (r == -EBUSY) { in kvm_check_and_inject_events()
10491 if (kvm_x86_call(get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10498 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10501 limit--; in process_nmi()
10503 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10504 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10506 if (vcpu->arch.nmi_pending && in process_nmi()
10508 vcpu->arch.nmi_pending--; in process_nmi()
10510 if (vcpu->arch.nmi_pending) in process_nmi()
10517 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10534 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10540 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10547 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10550 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10560 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10565 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10585 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10597 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10602 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10620 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10623 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10626 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10629 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10639 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10641 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10650 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10654 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10655 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10656 else if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10657 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10660 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10667 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10675 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10676 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10682 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10699 * Called within kvm->srcu read side.
10716 r = -EIO; in vcpu_enter_guest()
10726 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10736 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10760 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10761 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10772 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10778 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10781 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10782 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10789 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10806 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10807 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10808 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10809 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10810 vcpu->run->eoi.vector = in vcpu_enter_guest()
10811 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10824 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10825 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10826 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10831 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10832 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10833 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10840 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10841 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10848 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10849 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10866 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { in vcpu_enter_guest()
10875 ++vcpu->stat.req_event; in vcpu_enter_guest()
10881 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10916 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10917 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10922 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10925 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10946 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10962 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10963 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10965 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10967 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10968 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10969 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10970 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10981 * per-VM state, and responding vCPUs must wait for the update in vcpu_enter_guest()
11000 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
11001 ++vcpu->stat.exits; in vcpu_enter_guest()
11010 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
11011 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
11027 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
11028 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
11030 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11035 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
11038 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
11043 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11048 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
11055 ++vcpu->stat.exits; in vcpu_enter_guest()
11087 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
11090 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
11103 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
11111 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
11112 !vcpu->arch.apf.halted); in kvm_vcpu_running()
11117 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
11124 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
11131 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
11137 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
11155 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
11156 kvm_x86_ops.nested_ops->has_events(vcpu, false)) in kvm_vcpu_has_events()
11170 /* Called within kvm->srcu read side. */
11177 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
11180 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
11188 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
11209 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
11215 WARN_ON_ONCE(r == -EBUSY); in vcpu_block()
11222 switch(vcpu->arch.mp_state) { in vcpu_block()
11225 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
11226 vcpu->arch.mp_state = in vcpu_block()
11230 vcpu->arch.apf.halted = false; in vcpu_block()
11241 /* Called within kvm->srcu read side. */
11246 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; in vcpu_run()
11255 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11275 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11276 ++vcpu->stat.request_irq_exits; in vcpu_run()
11296 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
11301 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
11304 vcpu->arch.pv.pv_unhalted = false; in __kvm_emulate_halt()
11306 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
11309 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
11324 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
11366 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_preempted_in_kernel()
11371 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
11391 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11416 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11420 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11423 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11424 len = min(8u, frag->len); in complete_emulated_mmio()
11425 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11426 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11428 if (frag->len <= 8) { in complete_emulated_mmio()
11431 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11434 frag->data += len; in complete_emulated_mmio()
11435 frag->gpa += len; in complete_emulated_mmio()
11436 frag->len -= len; in complete_emulated_mmio()
11439 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11440 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11442 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11443 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11445 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11449 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11450 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11451 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11452 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11453 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11454 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11455 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11462 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11463 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11470 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11471 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11477 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11478 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11483 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11487 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11488 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11489 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11511 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11513 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11514 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11515 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11520 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || in kvm_arch_vcpu_ioctl_run()
11521 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { in kvm_arch_vcpu_ioctl_run()
11522 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11526 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11532 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11534 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11535 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11542 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11544 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11545 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11546 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11547 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11548 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11549 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11550 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11551 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11553 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11555 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11556 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11557 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11562 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11563 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11566 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11567 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11579 if (kvm_run->kvm_valid_regs) in kvm_arch_vcpu_ioctl_run()
11591 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11599 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11600 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11602 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11603 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11604 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11605 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11606 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11607 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11608 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11609 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11611 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11612 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11613 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11614 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11615 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11616 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11617 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11618 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11621 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11622 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11627 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_regs()
11628 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_regs()
11629 return -EINVAL; in kvm_arch_vcpu_ioctl_get_regs()
11639 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11640 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11642 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11643 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11644 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11645 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11646 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11647 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11648 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11649 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11651 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11652 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11653 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11654 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11655 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11656 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11657 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11658 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11661 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11662 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11664 vcpu->arch.exception.pending = false; in __set_regs()
11665 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11672 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_regs()
11673 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_regs()
11674 return -EINVAL; in kvm_arch_vcpu_ioctl_set_regs()
11686 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11689 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11690 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11691 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11692 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11693 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11694 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11696 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11697 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11700 sregs->idt.limit = dt.size; in __get_sregs_common()
11701 sregs->idt.base = dt.address; in __get_sregs_common()
11703 sregs->gdt.limit = dt.size; in __get_sregs_common()
11704 sregs->gdt.base = dt.address; in __get_sregs_common()
11706 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11707 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11710 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11711 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11712 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11713 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11714 sregs->apic_base = kvm_get_apic_base(vcpu); in __get_sregs_common()
11721 if (vcpu->arch.guest_state_protected) in __get_sregs()
11724 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11725 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11726 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11735 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11740 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11741 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11748 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_sregs()
11749 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_sregs()
11750 return -EINVAL; in kvm_arch_vcpu_ioctl_get_sregs()
11772 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11773 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11774 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11775 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11777 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11789 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11793 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11816 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11817 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11818 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11821 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11822 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11823 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11825 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11837 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11849 if (ret || vcpu->mmio_needed) { in kvm_task_switch()
11850 vcpu->mmio_needed = false; in kvm_task_switch()
11851 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11852 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11853 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11857 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11858 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11865 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11868 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11871 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11873 if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11877 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11878 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11880 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11884 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11885 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11896 return -EINVAL; in __set_sregs_common()
11898 apic_base_msr.data = sregs->apic_base; in __set_sregs_common()
11901 return -EINVAL; in __set_sregs_common()
11903 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11906 dt.size = sregs->idt.limit; in __set_sregs_common()
11907 dt.address = sregs->idt.base; in __set_sregs_common()
11909 dt.size = sregs->gdt.limit; in __set_sregs_common()
11910 dt.address = sregs->gdt.base; in __set_sregs_common()
11913 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11914 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11915 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11917 kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11919 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11921 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11922 kvm_x86_call(set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11924 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11925 kvm_x86_call(set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11927 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11928 kvm_x86_call(set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11931 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11936 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11939 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11940 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11941 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11942 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11943 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11944 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11946 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11947 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11953 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11955 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11976 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11989 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
11990 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
11991 !(sregs2->efer & EFER_LMA); in __set_sregs2()
11994 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
11995 return -EINVAL; in __set_sregs2()
11997 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
11998 return -EINVAL; in __set_sregs2()
12007 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
12011 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
12025 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_sregs()
12026 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_sregs()
12027 return -EINVAL; in kvm_arch_vcpu_ioctl_set_sregs()
12044 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12047 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12053 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12062 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
12063 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
12067 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12068 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
12071 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
12083 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
12084 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
12085 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
12087 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12089 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12090 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12093 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12097 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
12098 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
12108 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
12123 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
12129 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
12131 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
12132 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
12133 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
12134 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
12135 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
12145 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
12146 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_get_fpu()
12150 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
12151 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
12152 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
12153 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
12154 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
12155 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
12156 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
12157 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
12158 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
12168 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
12169 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_set_fpu()
12173 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
12175 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
12176 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
12177 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
12178 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
12179 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
12180 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
12181 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
12182 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
12192 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
12193 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
12195 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
12196 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
12198 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
12200 vcpu, &vcpu->run->s.regs.events); in store_regs()
12205 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
12206 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
12207 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
12210 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
12211 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
12214 return -EINVAL; in sync_regs()
12216 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
12219 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
12220 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
12223 return -EINVAL; in sync_regs()
12225 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
12233 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
12234 pr_warn_once("SMP vm created on host with unstable TSC; " in kvm_arch_vcpu_precreate()
12237 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12238 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
12240 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12241 return -EINVAL; in kvm_arch_vcpu_precreate()
12251 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
12252 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
12253 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
12255 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm); in kvm_arch_vcpu_create()
12257 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
12258 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
12260 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
12270 r = -ENOMEM; in kvm_arch_vcpu_create()
12275 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
12277 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
12279 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
12281 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
12283 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
12285 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
12292 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
12297 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); in kvm_arch_vcpu_create()
12298 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); in kvm_arch_vcpu_create()
12302 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
12305 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
12306 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
12309 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
12316 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
12317 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
12320 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
12327 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
12329 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
12331 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
12333 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
12334 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
12335 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
12345 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
12347 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12354 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12356 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12358 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12359 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12371 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12372 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12373 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12378 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12379 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12381 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12383 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12384 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12385 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12395 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12405 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12416 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12418 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12419 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12420 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12421 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12422 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12426 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12428 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12429 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12432 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12435 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12436 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12437 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12443 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12445 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12446 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12463 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12465 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_vcpu_reset()
12467 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12468 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12476 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12487 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12494 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12516 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12586 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_enable_virtualization_cpu()
12588 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_enable_virtualization_cpu()
12590 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_enable_virtualization_cpu()
12591 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_enable_virtualization_cpu()
12621 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_enable_virtualization_cpu()
12635 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_enable_virtualization_cpu()
12637 kvm->arch.backwards_tsc_observed = true; in kvm_arch_enable_virtualization_cpu()
12639 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_enable_virtualization_cpu()
12640 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_enable_virtualization_cpu()
12650 kvm->arch.last_tsc_nsec = 0; in kvm_arch_enable_virtualization_cpu()
12651 kvm->arch.last_tsc_write = 0; in kvm_arch_enable_virtualization_cpu()
12666 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12671 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12677 kfree(kvm->arch.hv_pa_pg); in kvm_arch_free_vm()
12689 return -EINVAL; in kvm_arch_init_vm()
12691 kvm->arch.vm_type = type; in kvm_arch_init_vm()
12692 kvm->arch.has_private_mem = in kvm_arch_init_vm()
12695 kvm->arch.pre_fault_allowed = in kvm_arch_init_vm()
12708 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12709 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12712 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12713 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12715 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12717 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12718 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12719 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12720 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12722 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12724 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12726 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12727 kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT; in kvm_arch_init_vm()
12728 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12729 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12732 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12733 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12736 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12737 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12777 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12778 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12795 * -errno: on error
12800 * GPA->HVA translation will not change. However, the HVA is a user
12812 /* Called with kvm->slots_lock held. */ in __x86_set_memory_region()
12814 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12818 if (slot && slot->npages) in __x86_set_memory_region()
12819 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12830 if (!slot || !slot->npages) in __x86_set_memory_region()
12833 old_npages = slot->npages; in __x86_set_memory_region()
12834 hva = slot->userspace_addr; in __x86_set_memory_region()
12864 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12870 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12876 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12880 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12884 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12885 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12897 vfree(slot->arch.rmap[i]); in memslot_rmap_free()
12898 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12909 vfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12910 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12918 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12925 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12928 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12929 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12931 return -ENOMEM; in memslot_rmap_alloc()
12941 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12949 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12969 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12971 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12973 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12974 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12975 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12980 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
13001 vfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
13002 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
13004 return -ENOMEM; in kvm_alloc_memslot_metadata()
13013 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
13018 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
13033 return -EINVAL; in kvm_arch_prepare_memory_region()
13036 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
13037 return -EINVAL; in kvm_arch_prepare_memory_region()
13043 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
13045 return -EIO; in kvm_arch_prepare_memory_region()
13058 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
13068 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
13069 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
13089 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
13098 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
13116 * which can be collapsed into a single large-page spte. Later in kvm_mmu_slot_apply_flags()
13117 * page faults will create the large-page sptes. in kvm_mmu_slot_apply_flags()
13122 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
13147 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
13154 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
13163 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
13164 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
13165 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
13166 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
13168 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
13171 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
13172 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
13175 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
13178 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
13192 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
13196 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
13210 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
13234 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
13255 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
13263 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
13264 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13285 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13292 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13295 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13304 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13305 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13313 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13322 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13326 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13329 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13331 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13338 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13347 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13355 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13364 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13377 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13386 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13404 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13419 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13420 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13428 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13451 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13454 if (work->wakeup_all) in kvm_arch_async_page_present()
13455 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13457 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13458 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13460 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13462 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13463 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13467 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13468 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13474 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13488 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13495 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13501 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13508 * Non-coherent DMA assignment and de-assignment may affect whether or in kvm_noncoherent_dma_assignment_start_or_stop()
13511 * (or last) non-coherent device is (un)registered to so that new SPTEs in kvm_noncoherent_dma_assignment_start_or_stop()
13520 if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) in kvm_arch_register_noncoherent_dma()
13527 if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) in kvm_arch_unregister_noncoherent_dma()
13534 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13550 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13551 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13552 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13553 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13555 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13567 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13568 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13572 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13576 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_del_producer()
13577 prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13580 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13582 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13594 if (new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13597 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13607 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13653 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13659 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13661 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13672 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13685 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13686 return -EIO; in kvm_handle_memory_failure()
13696 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13752 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13771 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13775 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13778 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13779 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13780 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13781 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13783 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13786 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13789 frag->data += len; in complete_sev_es_emulated_mmio()
13790 frag->gpa += len; in complete_sev_es_emulated_mmio()
13791 frag->len -= len; in complete_sev_es_emulated_mmio()
13794 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13795 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13803 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13804 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13805 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13806 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13807 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13808 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13810 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13822 return -EINVAL; in kvm_sev_es_mmio_write()
13828 bytes -= handled; in kvm_sev_es_mmio_write()
13833 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13834 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13835 frag->len = bytes; in kvm_sev_es_mmio_write()
13836 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13837 frag->data = data; in kvm_sev_es_mmio_write()
13839 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13840 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13842 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13843 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13844 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13845 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13846 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13848 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13861 return -EINVAL; in kvm_sev_es_mmio_read()
13867 bytes -= handled; in kvm_sev_es_mmio_read()
13872 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13873 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13874 frag->len = bytes; in kvm_sev_es_mmio_read()
13875 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13876 frag->data = data; in kvm_sev_es_mmio_read()
13878 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13879 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13881 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13882 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13883 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13884 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13886 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13894 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13895 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13903 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13904 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13906 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13907 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13917 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13918 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13926 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13930 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13939 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13940 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13941 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13943 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13945 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13955 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13956 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13961 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13965 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13973 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13974 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()