Lines Matching +full:led +full:- +full:7 +full:seg

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
16 * Ben-Ami Yassour <benami@il.ibm.com>
48 #include <linux/user-return-notifier.h>
62 #include <linux/entry-kvm.h>
109 ((struct kvm_vcpu *)(ctxt)->vcpu)
112 * - enable syscall per default because its emulated by KVM
113 * - enable LME and LMA per default on 64 bit KVM
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;
349 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
353 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
445 * List of MSRs that control the existence of MSR-based features, i.e. MSRs
458 (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
550 size - useroffset, NULL); in kvm_alloc_emulator_cache()
559 vcpu->arch.apf.gfns[i] = ~0; in kvm_async_pf_hash_reset()
575 if (msrs->registered) { in kvm_on_user_return()
576 msrs->registered = false; in kvm_on_user_return()
581 values = &msrs->values[slot]; in kvm_on_user_return()
582 if (values->host != values->curr) { in kvm_on_user_return()
583 wrmsrl(kvm_uret_msrs_list[slot], values->host); in kvm_on_user_return()
584 values->curr = values->host; in kvm_on_user_return()
609 return -1; in kvm_add_user_return_msr()
624 return -1; in kvm_find_user_return_msr()
636 msrs->values[i].host = value; in kvm_user_return_msr_cpu_online()
637 msrs->values[i].curr = value; in kvm_user_return_msr_cpu_online()
646 value = (value & mask) | (msrs->values[slot].host & ~mask); in kvm_set_user_return_msr()
647 if (value == msrs->values[slot].curr) in kvm_set_user_return_msr()
653 msrs->values[slot].curr = value; in kvm_set_user_return_msr()
654 if (!msrs->registered) { in kvm_set_user_return_msr()
655 msrs->urn.on_user_return = kvm_on_user_return; in kvm_set_user_return_msr()
656 user_return_notifier_register(&msrs->urn); in kvm_set_user_return_msr()
657 msrs->registered = true; in kvm_set_user_return_msr()
667 if (msrs->registered) in drop_user_return_notifiers()
668 kvm_on_user_return(&msrs->urn); in drop_user_return_notifiers()
722 * #DBs can be trap-like or fault-like, the caller must check other CPU in exception_type()
741 if (!ex->has_payload) in kvm_deliver_exception_payload()
744 switch (ex->vector) { in kvm_deliver_exception_payload()
747 * "Certain debug exceptions may clear bit 0-3. The in kvm_deliver_exception_payload()
751 vcpu->arch.dr6 &= ~DR_TRAP_BITS; in kvm_deliver_exception_payload()
760 * Active low bits should be cleared if 1-setting in payload. in kvm_deliver_exception_payload()
761 * Active high bits should be set if 1-setting in payload. in kvm_deliver_exception_payload()
768 vcpu->arch.dr6 |= DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
769 vcpu->arch.dr6 |= ex->payload; in kvm_deliver_exception_payload()
770 vcpu->arch.dr6 ^= ex->payload & DR6_ACTIVE_LOW; in kvm_deliver_exception_payload()
778 vcpu->arch.dr6 &= ~BIT(12); in kvm_deliver_exception_payload()
781 vcpu->arch.cr2 = ex->payload; in kvm_deliver_exception_payload()
785 ex->has_payload = false; in kvm_deliver_exception_payload()
786 ex->payload = 0; in kvm_deliver_exception_payload()
794 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit; in kvm_queue_exception_vmexit()
796 ex->vector = vector; in kvm_queue_exception_vmexit()
797 ex->injected = false; in kvm_queue_exception_vmexit()
798 ex->pending = true; in kvm_queue_exception_vmexit()
799 ex->has_error_code = has_error_code; in kvm_queue_exception_vmexit()
800 ex->error_code = error_code; in kvm_queue_exception_vmexit()
801 ex->has_payload = has_payload; in kvm_queue_exception_vmexit()
802 ex->payload = payload; in kvm_queue_exception_vmexit()
816 * morph it to a VM-Exit if L1 wants to intercept the exception. A in kvm_multiple_exception()
818 * when it was original queued, and re-checking is incorrect if _L1_ in kvm_multiple_exception()
822 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, nr, error_code)) { in kvm_multiple_exception()
828 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { in kvm_multiple_exception()
832 * On VM-Entry, an exception can be pending if and only in kvm_multiple_exception()
839 vcpu->arch.exception.injected = true; in kvm_multiple_exception()
849 vcpu->arch.exception.pending = true; in kvm_multiple_exception()
850 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
852 vcpu->arch.exception.has_error_code = has_error; in kvm_multiple_exception()
853 vcpu->arch.exception.vector = nr; in kvm_multiple_exception()
854 vcpu->arch.exception.error_code = error_code; in kvm_multiple_exception()
855 vcpu->arch.exception.has_payload = has_payload; in kvm_multiple_exception()
856 vcpu->arch.exception.payload = payload; in kvm_multiple_exception()
859 &vcpu->arch.exception); in kvm_multiple_exception()
864 prev_nr = vcpu->arch.exception.vector; in kvm_multiple_exception()
866 /* triple fault -> shutdown */ in kvm_multiple_exception()
878 vcpu->arch.exception.injected = false; in kvm_multiple_exception()
879 vcpu->arch.exception.pending = false; in kvm_multiple_exception()
884 that instruction re-execution will regenerate lost in kvm_multiple_exception()
940 ++vcpu->stat.pf_guest; in kvm_inject_page_fault()
943 * Async #PF in L2 is always forwarded to L1 as a VM-Exit regardless of in kvm_inject_page_fault()
946 if (is_guest_mode(vcpu) && fault->async_page_fault) in kvm_inject_page_fault()
948 true, fault->error_code, in kvm_inject_page_fault()
949 true, fault->address); in kvm_inject_page_fault()
951 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, in kvm_inject_page_fault()
952 fault->address); in kvm_inject_page_fault()
959 WARN_ON_ONCE(fault->vector != PF_VECTOR); in kvm_inject_emulated_page_fault()
961 fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : in kvm_inject_emulated_page_fault()
962 vcpu->arch.walk_mmu; in kvm_inject_emulated_page_fault()
968 if ((fault->error_code & PFERR_PRESENT_MASK) && in kvm_inject_emulated_page_fault()
969 !(fault->error_code & PFERR_RSVD_MASK)) in kvm_inject_emulated_page_fault()
970 kvm_mmu_invalidate_addr(vcpu, fault_mmu, fault->address, in kvm_inject_emulated_page_fault()
973 fault_mmu->inject_page_fault(vcpu, fault); in kvm_inject_emulated_page_fault()
979 atomic_inc(&vcpu->arch.nmi_queued); in kvm_inject_nmi()
1019 return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2); in pdptr_rsvd_bits()
1027 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in load_pdptrs()
1032 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; in load_pdptrs()
1060 if (!tdp_enabled && memcmp(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs))) in load_pdptrs()
1061 kvm_mmu_free_roots(vcpu->kvm, mmu, KVM_MMU_ROOT_CURRENT); in load_pdptrs()
1063 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); in load_pdptrs()
1066 vcpu->arch.pdptrs_from_userspace = false; in load_pdptrs()
1091 * CR0.WP is incorporated into the MMU role, but only for non-nested, in kvm_post_set_cr0()
1138 if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && in kvm_set_cr0()
1149 if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && in kvm_set_cr0()
1174 if (vcpu->arch.guest_state_protected) in kvm_load_guest_xsave_state()
1179 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_guest_xsave_state()
1180 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); in kvm_load_guest_xsave_state()
1183 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_guest_xsave_state()
1184 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); in kvm_load_guest_xsave_state()
1188 vcpu->arch.pkru != vcpu->arch.host_pkru && in kvm_load_guest_xsave_state()
1189 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_guest_xsave_state()
1191 write_pkru(vcpu->arch.pkru); in kvm_load_guest_xsave_state()
1197 if (vcpu->arch.guest_state_protected) in kvm_load_host_xsave_state()
1201 ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) || in kvm_load_host_xsave_state()
1203 vcpu->arch.pkru = rdpkru(); in kvm_load_host_xsave_state()
1204 if (vcpu->arch.pkru != vcpu->arch.host_pkru) in kvm_load_host_xsave_state()
1205 write_pkru(vcpu->arch.host_pkru); in kvm_load_host_xsave_state()
1210 if (vcpu->arch.xcr0 != kvm_host.xcr0) in kvm_load_host_xsave_state()
1214 vcpu->arch.ia32_xss != kvm_host.xss) in kvm_load_host_xsave_state()
1224 return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; in kvm_guest_supported_xfd()
1231 u64 old_xcr0 = vcpu->arch.xcr0; in __kvm_set_xcr()
1247 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; in __kvm_set_xcr()
1266 vcpu->arch.xcr0 = xcr0; in __kvm_set_xcr()
1291 if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) in __kvm_is_valid_cr4()
1310 * If CR4.PCIDE is changed 0 -> 1, there is no need to flush the TLB in kvm_post_set_cr4()
1324 * - CR4.PCIDE is changed from 1 to 0 in kvm_post_set_cr4()
1325 * - CR4.PGE is toggled in kvm_post_set_cr4()
1336 * - CR4.SMEP is changed from 0 to 1 in kvm_post_set_cr4()
1337 * - CR4.PAE is toggled in kvm_post_set_cr4()
1379 struct kvm_mmu *mmu = vcpu->arch.mmu; in kvm_invalidate_pcid()
1414 if (kvm_get_pcid(vcpu, mmu->prev_roots[i].pgd) == pcid) in kvm_invalidate_pcid()
1417 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free); in kvm_invalidate_pcid()
1450 vcpu->arch.cr3 = cr3; in kvm_set_cr3()
1459 * and it's impossible to use a non-zero PCID when PCID is disabled, in kvm_set_cr3()
1476 vcpu->arch.cr8 = cr8; in kvm_set_cr8()
1486 return vcpu->arch.cr8; in kvm_get_cr8()
1494 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { in kvm_update_dr0123()
1496 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_update_dr0123()
1504 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) in kvm_update_dr7()
1505 dr7 = vcpu->arch.guest_debug_dr7; in kvm_update_dr7()
1507 dr7 = vcpu->arch.dr7; in kvm_update_dr7()
1509 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1511 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; in kvm_update_dr7()
1529 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_set_dr()
1533 vcpu->arch.db[array_index_nospec(dr, size)] = val; in kvm_set_dr()
1534 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) in kvm_set_dr()
1535 vcpu->arch.eff_db[dr] = val; in kvm_set_dr()
1541 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); in kvm_set_dr()
1544 default: /* 7 */ in kvm_set_dr()
1547 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; in kvm_set_dr()
1558 size_t size = ARRAY_SIZE(vcpu->arch.db); in kvm_get_dr()
1562 return vcpu->arch.db[array_index_nospec(dr, size)]; in kvm_get_dr()
1565 return vcpu->arch.dr6; in kvm_get_dr()
1567 default: /* 7 */ in kvm_get_dr()
1568 return vcpu->arch.dr7; in kvm_get_dr()
1592 * 10 - MISC_PACKAGE_CTRLS
1593 * 11 - ENERGY_FILTERING_CTL
1594 * 12 - DOITM
1595 * 18 - FB_CLEAR_CTRL
1596 * 21 - XAPIC_DISABLE_STATUS
1597 * 23 - OVERCLOCKING_STATUS
1728 u64 old_efer = vcpu->arch.efer; in set_efer()
1729 u64 efer = msr_info->data; in set_efer()
1735 if (!msr_info->host_initiated) { in set_efer()
1740 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) in set_efer()
1745 efer |= vcpu->arch.efer & EFER_LMA; in set_efer()
1773 struct kvm *kvm = vcpu->kvm; in kvm_msr_allowed()
1782 idx = srcu_read_lock(&kvm->srcu); in kvm_msr_allowed()
1784 msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); in kvm_msr_allowed()
1790 allowed = msr_filter->default_allow; in kvm_msr_allowed()
1791 ranges = msr_filter->ranges; in kvm_msr_allowed()
1793 for (i = 0; i < msr_filter->count; i++) { in kvm_msr_allowed()
1800 allowed = test_bit(index - start, bitmap); in kvm_msr_allowed()
1806 srcu_read_unlock(&kvm->srcu, idx); in kvm_msr_allowed()
1815 * Returns 0 on success, non-0 otherwise.
1836 * non-canonical address is written on Intel but not on in __kvm_set_msr()
1837 * AMD (which ignores the top 32-bits, because it does in __kvm_set_msr()
1838 * not implement 64-bit SYSENTER). in __kvm_set_msr()
1840 * 64-bit code should hence be able to write a non-canonical in __kvm_set_msr()
1842 * vmentry does not fail on Intel after writing a non-canonical in __kvm_set_msr()
1844 * invokes 64-bit SYSENTER. in __kvm_set_msr()
1863 * clear the bits. This ensures cross-vendor migration will in __kvm_set_msr()
1896 * Returns 0 on success, non-0 otherwise.
1963 if (!vcpu->run->msr.error) { in complete_userspace_rdmsr()
1964 kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); in complete_userspace_rdmsr()
1965 kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); in complete_userspace_rdmsr()
1971 return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error); in complete_emulated_msr_access()
1982 return kvm_x86_call(complete_emulated_msr)(vcpu, vcpu->run->msr.error); in complete_fast_msr_access()
2011 if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) in kvm_msr_user_space()
2014 vcpu->run->exit_reason = exit_reason; in kvm_msr_user_space()
2015 vcpu->run->msr.error = 0; in kvm_msr_user_space()
2016 memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); in kvm_msr_user_space()
2017 vcpu->run->msr.reason = msr_reason; in kvm_msr_user_space()
2018 vcpu->run->msr.index = index; in kvm_msr_user_space()
2019 vcpu->run->msr.data = data; in kvm_msr_user_space()
2020 vcpu->arch.complete_userspace_io = completion; in kvm_msr_user_space()
2036 kvm_rax_write(vcpu, data & -1u); in kvm_emulate_rdmsr()
2037 kvm_rdx_write(vcpu, (data >> 32) & -1u); in kvm_emulate_rdmsr()
2097 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) && in kvm_emulate_monitor_mwait()
2120 return READ_ONCE(vcpu->mode) == EXITING_GUEST_MODE || in kvm_vcpu_exit_request()
2126 * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces
2133 if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) in handle_fastpath_set_x2apic_icr_irqoff()
2140 return kvm_x2apic_icr_write(vcpu->arch.apic, data); in handle_fastpath_set_x2apic_icr_irqoff()
2214 return -EINVAL; in do_set_msr()
2246 write_seqcount_begin(&vdata->seq); in update_pvclock_gtod()
2249 vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; in update_pvclock_gtod()
2250 vdata->clock.cycle_last = tk->tkr_mono.cycle_last; in update_pvclock_gtod()
2251 vdata->clock.mask = tk->tkr_mono.mask; in update_pvclock_gtod()
2252 vdata->clock.mult = tk->tkr_mono.mult; in update_pvclock_gtod()
2253 vdata->clock.shift = tk->tkr_mono.shift; in update_pvclock_gtod()
2254 vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; in update_pvclock_gtod()
2255 vdata->clock.offset = tk->tkr_mono.base; in update_pvclock_gtod()
2257 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; in update_pvclock_gtod()
2258 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; in update_pvclock_gtod()
2259 vdata->raw_clock.mask = tk->tkr_raw.mask; in update_pvclock_gtod()
2260 vdata->raw_clock.mult = tk->tkr_raw.mult; in update_pvclock_gtod()
2261 vdata->raw_clock.shift = tk->tkr_raw.shift; in update_pvclock_gtod()
2262 vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; in update_pvclock_gtod()
2263 vdata->raw_clock.offset = tk->tkr_raw.base; in update_pvclock_gtod()
2265 vdata->wall_time_sec = tk->xtime_sec; in update_pvclock_gtod()
2267 vdata->offs_boot = tk->offs_boot; in update_pvclock_gtod()
2269 write_seqcount_end(&vdata->seq); in update_pvclock_gtod()
2329 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_write_system_time()
2331 if (vcpu->vcpu_id == 0 && !host_initiated) { in kvm_write_system_time()
2332 if (ka->boot_vcpu_runs_old_kvmclock != old_msr) in kvm_write_system_time()
2335 ka->boot_vcpu_runs_old_kvmclock = old_msr; in kvm_write_system_time()
2338 vcpu->arch.time = system_time; in kvm_write_system_time()
2343 kvm_gpc_activate(&vcpu->arch.pv_time, system_time & ~1ULL, in kvm_write_system_time()
2346 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvm_write_system_time()
2369 shift--; in kvm_get_time_scale()
2414 vcpu->arch.tsc_catchup = 1; in set_tsc_khz()
2415 vcpu->arch.tsc_always_catchup = 1; in set_tsc_khz()
2419 return -1; in set_tsc_khz()
2423 /* TSC scaling required - calculate ratio */ in set_tsc_khz()
2428 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", in set_tsc_khz()
2430 return -1; in set_tsc_khz()
2446 return -1; in kvm_set_tsc_khz()
2451 &vcpu->arch.virtual_tsc_shift, in kvm_set_tsc_khz()
2452 &vcpu->arch.virtual_tsc_mult); in kvm_set_tsc_khz()
2453 vcpu->arch.virtual_tsc_khz = user_tsc_khz; in kvm_set_tsc_khz()
2461 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); in kvm_set_tsc_khz()
2473 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, in compute_guest_tsc()
2474 vcpu->arch.virtual_tsc_mult, in compute_guest_tsc()
2475 vcpu->arch.virtual_tsc_shift); in compute_guest_tsc()
2476 tsc += vcpu->arch.this_tsc_write; in compute_guest_tsc()
2490 struct kvm_arch *ka = &vcpu->kvm->arch; in kvm_track_tsc_matching()
2498 bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 == in kvm_track_tsc_matching()
2499 atomic_read(&vcpu->kvm->online_vcpus)) && in kvm_track_tsc_matching()
2500 gtod_is_based_on_tsc(gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2508 if ((ka->use_master_clock && new_generation) || in kvm_track_tsc_matching()
2509 (ka->use_master_clock != use_master_clock)) in kvm_track_tsc_matching()
2512 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, in kvm_track_tsc_matching()
2513 atomic_read(&vcpu->kvm->online_vcpus), in kvm_track_tsc_matching()
2514 ka->use_master_clock, gtod->clock.vclock_mode); in kvm_track_tsc_matching()
2521 * The most significant 64-N bits (mult) of ratio represent the
2524 * point number (mult + frac * 2^(-N)).
2547 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); in kvm_compute_l1_tsc_offset()
2549 return target_tsc - tsc; in kvm_compute_l1_tsc_offset()
2554 return vcpu->arch.l1_tsc_offset + in kvm_read_l1_tsc()
2555 kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); in kvm_read_l1_tsc()
2586 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in kvm_vcpu_write_tsc_offset()
2587 vcpu->arch.l1_tsc_offset, in kvm_vcpu_write_tsc_offset()
2590 vcpu->arch.l1_tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2598 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( in kvm_vcpu_write_tsc_offset()
2603 vcpu->arch.tsc_offset = l1_offset; in kvm_vcpu_write_tsc_offset()
2610 vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2614 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( in kvm_vcpu_write_tsc_multiplier()
2618 vcpu->arch.tsc_scaling_ratio = l1_multiplier; in kvm_vcpu_write_tsc_multiplier()
2628 * TSC is marked unstable when we're running on Hyper-V, in kvm_check_tsc_unstable()
2645 struct kvm *kvm = vcpu->kvm; in __kvm_synchronize_tsc()
2647 lockdep_assert_held(&kvm->arch.tsc_write_lock); in __kvm_synchronize_tsc()
2653 kvm->arch.last_tsc_nsec = ns; in __kvm_synchronize_tsc()
2654 kvm->arch.last_tsc_write = tsc; in __kvm_synchronize_tsc()
2655 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; in __kvm_synchronize_tsc()
2656 kvm->arch.last_tsc_offset = offset; in __kvm_synchronize_tsc()
2658 vcpu->arch.last_guest_tsc = tsc; in __kvm_synchronize_tsc()
2670 * These values are tracked in kvm->arch.cur_xxx variables. in __kvm_synchronize_tsc()
2672 kvm->arch.cur_tsc_generation++; in __kvm_synchronize_tsc()
2673 kvm->arch.cur_tsc_nsec = ns; in __kvm_synchronize_tsc()
2674 kvm->arch.cur_tsc_write = tsc; in __kvm_synchronize_tsc()
2675 kvm->arch.cur_tsc_offset = offset; in __kvm_synchronize_tsc()
2676 kvm->arch.nr_vcpus_matched_tsc = 0; in __kvm_synchronize_tsc()
2677 } else if (vcpu->arch.this_tsc_generation != kvm->arch.cur_tsc_generation) { in __kvm_synchronize_tsc()
2678 kvm->arch.nr_vcpus_matched_tsc++; in __kvm_synchronize_tsc()
2682 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; in __kvm_synchronize_tsc()
2683 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; in __kvm_synchronize_tsc()
2684 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; in __kvm_synchronize_tsc()
2692 struct kvm *kvm = vcpu->kvm; in kvm_synchronize_tsc()
2698 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2701 elapsed = ns - kvm->arch.last_tsc_nsec; in kvm_synchronize_tsc()
2703 if (vcpu->arch.virtual_tsc_khz) { in kvm_synchronize_tsc()
2710 } else if (kvm->arch.user_set_tsc) { in kvm_synchronize_tsc()
2711 u64 tsc_exp = kvm->arch.last_tsc_write + in kvm_synchronize_tsc()
2713 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; in kvm_synchronize_tsc()
2715 * Here lies UAPI baggage: when a user-initiated TSC write has in kvm_synchronize_tsc()
2726 * come from the kernel's default vCPU creation. Make the 1-second in kvm_synchronize_tsc()
2735 kvm->arch.user_set_tsc = true; in kvm_synchronize_tsc()
2744 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { in kvm_synchronize_tsc()
2746 offset = kvm->arch.cur_tsc_offset; in kvm_synchronize_tsc()
2756 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_synchronize_tsc()
2762 u64 tsc_offset = vcpu->arch.l1_tsc_offset; in adjust_tsc_offset_guest()
2768 if (vcpu->arch.l1_tsc_scaling_ratio != kvm_caps.default_tsc_scaling_ratio) in adjust_tsc_offset_host()
2771 vcpu->arch.l1_tsc_scaling_ratio); in adjust_tsc_offset_host()
2803 switch (clock->vclock_mode) { in vgettsc()
2809 v = (tsc_pg_val - clock->cycle_last) & in vgettsc()
2810 clock->mask; in vgettsc()
2819 v = (*tsc_timestamp - clock->cycle_last) & in vgettsc()
2820 clock->mask; in vgettsc()
2829 return v * clock->mult; in vgettsc()
2834 * frequency of CLOCK_MONOTONIC_RAW (hence adding gtos->offs_boot).
2844 seq = read_seqcount_begin(&gtod->seq); in do_kvmclock_base()
2845 ns = gtod->raw_clock.base_cycles; in do_kvmclock_base()
2846 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode); in do_kvmclock_base()
2847 ns >>= gtod->raw_clock.shift; in do_kvmclock_base()
2848 ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); in do_kvmclock_base()
2849 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_kvmclock_base()
2867 seq = read_seqcount_begin(&gtod->seq); in do_monotonic()
2868 ns = gtod->clock.base_cycles; in do_monotonic()
2869 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_monotonic()
2870 ns >>= gtod->clock.shift; in do_monotonic()
2871 ns += ktime_to_ns(gtod->clock.offset); in do_monotonic()
2872 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_monotonic()
2886 seq = read_seqcount_begin(&gtod->seq); in do_realtime()
2887 ts->tv_sec = gtod->wall_time_sec; in do_realtime()
2888 ns = gtod->clock.base_cycles; in do_realtime()
2889 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode); in do_realtime()
2890 ns >>= gtod->clock.shift; in do_realtime()
2891 } while (unlikely(read_seqcount_retry(&gtod->seq, seq))); in do_realtime()
2893 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); in do_realtime()
2894 ts->tv_nsec = ns; in do_realtime()
2962 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2963 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2964 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2968 * - ret0 < ret1
2969 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2971 * - 0 < N - M => M < N
2990 struct kvm_arch *ka = &kvm->arch; in pvclock_update_vm_gtod_copy()
2994 lockdep_assert_held(&kvm->arch.tsc_write_lock); in pvclock_update_vm_gtod_copy()
2995 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == in pvclock_update_vm_gtod_copy()
2996 atomic_read(&kvm->online_vcpus)); in pvclock_update_vm_gtod_copy()
3003 &ka->master_kernel_ns, in pvclock_update_vm_gtod_copy()
3004 &ka->master_cycle_now); in pvclock_update_vm_gtod_copy()
3006 ka->use_master_clock = host_tsc_clocksource && vcpus_matched in pvclock_update_vm_gtod_copy()
3007 && !ka->backwards_tsc_observed in pvclock_update_vm_gtod_copy()
3008 && !ka->boot_vcpu_runs_old_kvmclock; in pvclock_update_vm_gtod_copy()
3010 if (ka->use_master_clock) in pvclock_update_vm_gtod_copy()
3014 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, in pvclock_update_vm_gtod_copy()
3026 raw_spin_lock_irq(&kvm->arch.tsc_write_lock); in __kvm_start_pvclock_update()
3027 write_seqcount_begin(&kvm->arch.pvclock_sc); in __kvm_start_pvclock_update()
3040 struct kvm_arch *ka = &kvm->arch; in kvm_end_pvclock_update()
3044 write_seqcount_end(&ka->pvclock_sc); in kvm_end_pvclock_update()
3045 raw_spin_unlock_irq(&ka->tsc_write_lock); in kvm_end_pvclock_update()
3064 * per-CPU value (which may be zero if a CPU is going offline). Note, tsc_khz
3078 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */
3081 struct kvm_arch *ka = &kvm->arch; in __get_kvmclock()
3087 data->flags = 0; in __get_kvmclock()
3088 if (ka->use_master_clock && in __get_kvmclock()
3093 if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { in __get_kvmclock()
3094 data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; in __get_kvmclock()
3095 data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; in __get_kvmclock()
3098 data->host_tsc = rdtsc(); in __get_kvmclock()
3100 data->flags |= KVM_CLOCK_TSC_STABLE; in __get_kvmclock()
3101 hv_clock.tsc_timestamp = ka->master_cycle_now; in __get_kvmclock()
3102 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in __get_kvmclock()
3106 data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); in __get_kvmclock()
3108 data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; in __get_kvmclock()
3116 struct kvm_arch *ka = &kvm->arch; in get_kvmclock()
3120 seq = read_seqcount_begin(&ka->pvclock_sc); in get_kvmclock()
3122 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in get_kvmclock()
3138 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_setup_guest_pvclock()
3142 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3144 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3149 read_lock_irqsave(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3152 guest_hv_clock = (void *)(gpc->khva + offset); in kvm_setup_guest_pvclock()
3161 guest_hv_clock->version = vcpu->hv_clock.version = (guest_hv_clock->version + 1) | 1; in kvm_setup_guest_pvclock()
3165 vcpu->hv_clock.flags |= (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED); in kvm_setup_guest_pvclock()
3167 if (vcpu->pvclock_set_guest_stopped_request) { in kvm_setup_guest_pvclock()
3168 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; in kvm_setup_guest_pvclock()
3169 vcpu->pvclock_set_guest_stopped_request = false; in kvm_setup_guest_pvclock()
3172 memcpy(guest_hv_clock, &vcpu->hv_clock, sizeof(*guest_hv_clock)); in kvm_setup_guest_pvclock()
3175 guest_hv_clock->flags &= ~PVCLOCK_TSC_STABLE_BIT; in kvm_setup_guest_pvclock()
3179 guest_hv_clock->version = ++vcpu->hv_clock.version; in kvm_setup_guest_pvclock()
3182 read_unlock_irqrestore(&gpc->lock, flags); in kvm_setup_guest_pvclock()
3184 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); in kvm_setup_guest_pvclock()
3191 struct kvm_vcpu_arch *vcpu = &v->arch; in kvm_guest_time_update()
3192 struct kvm_arch *ka = &v->kvm->arch; in kvm_guest_time_update()
3201 * This default behaviour led to bugs in some guest kernels which cause in kvm_guest_time_update()
3205 ka->xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_PVCLOCK_TSC_UNSTABLE; in kvm_guest_time_update()
3216 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_guest_time_update()
3217 use_master_clock = ka->use_master_clock; in kvm_guest_time_update()
3219 host_tsc = ka->master_cycle_now; in kvm_guest_time_update()
3220 kernel_ns = ka->master_kernel_ns; in kvm_guest_time_update()
3222 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_guest_time_update()
3249 if (vcpu->tsc_catchup) { in kvm_guest_time_update()
3252 adjust_tsc_offset_guest(v, tsc - tsc_timestamp); in kvm_guest_time_update()
3263 v->arch.l1_tsc_scaling_ratio); in kvm_guest_time_update()
3265 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { in kvm_guest_time_update()
3267 &vcpu->hv_clock.tsc_shift, in kvm_guest_time_update()
3268 &vcpu->hv_clock.tsc_to_system_mul); in kvm_guest_time_update()
3269 vcpu->hw_tsc_khz = tgt_tsc_khz; in kvm_guest_time_update()
3273 vcpu->hv_clock.tsc_timestamp = tsc_timestamp; in kvm_guest_time_update()
3274 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; in kvm_guest_time_update()
3275 vcpu->last_guest_tsc = tsc_timestamp; in kvm_guest_time_update()
3282 vcpu->hv_clock.flags = pvclock_flags; in kvm_guest_time_update()
3284 if (vcpu->pv_time.active) in kvm_guest_time_update()
3285 kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0, false); in kvm_guest_time_update()
3287 if (vcpu->xen.vcpu_info_cache.active) in kvm_guest_time_update()
3288 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, in kvm_guest_time_update()
3291 if (vcpu->xen.vcpu_time_info_cache.active) in kvm_guest_time_update()
3292 kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0, in kvm_guest_time_update()
3295 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); in kvm_guest_time_update()
3322 struct kvm_arch *ka = &kvm->arch; in kvm_get_wall_clock_epoch()
3328 seq = read_seqcount_begin(&ka->pvclock_sc); in kvm_get_wall_clock_epoch()
3331 if (!ka->use_master_clock) in kvm_get_wall_clock_epoch()
3353 hv_clock.tsc_timestamp = ka->master_cycle_now; in kvm_get_wall_clock_epoch()
3354 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; in kvm_get_wall_clock_epoch()
3356 } while (read_seqcount_retry(&ka->pvclock_sc, seq)); in kvm_get_wall_clock_epoch()
3362 * since 1970-01-01. in kvm_get_wall_clock_epoch()
3368 return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - in kvm_get_wall_clock_epoch()
3372 return ktime_get_real_ns() - get_kvmclock_ns(kvm); in kvm_get_wall_clock_epoch()
3377 * vcpu->cpu migration, should not allow system_timestamp from
3383 * We need to rate-limit these requests though, as they can
3386 * by the delay we use to rate-limit the updates.
3408 struct kvm *kvm = v->kvm; in kvm_gen_kvmclock_update()
3411 schedule_delayed_work(&kvm->arch.kvmclock_update_work, in kvm_gen_kvmclock_update()
3424 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); in kvmclock_sync_fn()
3425 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvmclock_sync_fn()
3446 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); in can_set_mci_status()
3453 u64 mcg_cap = vcpu->arch.mcg_cap; in set_msr_mce()
3455 u32 msr = msr_info->index; in set_msr_mce()
3456 u64 data = msr_info->data; in set_msr_mce()
3461 vcpu->arch.mcg_status = data; in set_msr_mce()
3465 (data || !msr_info->host_initiated)) in set_msr_mce()
3469 vcpu->arch.mcg_ctl = data; in set_msr_mce()
3471 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3472 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in set_msr_mce()
3476 if (!(mcg_cap & MCG_CMCI_P) && (data || !msr_info->host_initiated)) in set_msr_mce()
3481 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in set_msr_mce()
3482 last_msr + 1 - MSR_IA32_MC0_CTL2); in set_msr_mce()
3483 vcpu->arch.mci_ctl2_banks[offset] = data; in set_msr_mce()
3485 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in set_msr_mce()
3486 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in set_msr_mce()
3498 * single-bit ECC data errors. in set_msr_mce()
3506 * AMD-based CPUs allow non-zero values, but if and only if in set_msr_mce()
3509 if (!msr_info->host_initiated && is_mci_status_msr(msr) && in set_msr_mce()
3513 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in set_msr_mce()
3514 last_msr + 1 - MSR_IA32_MC0_CTL); in set_msr_mce()
3515 vcpu->arch.mce_banks[offset] = data; in set_msr_mce()
3527 return (vcpu->arch.apf.msr_en_val & mask) == mask; in kvm_pv_async_pf_enabled()
3549 vcpu->arch.apf.msr_en_val = data; in kvm_pv_enable_async_pf()
3557 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, in kvm_pv_enable_async_pf()
3561 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); in kvm_pv_enable_async_pf()
3562 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; in kvm_pv_enable_async_pf()
3571 /* Bits 8-63 are reserved */ in kvm_pv_enable_async_pf_int()
3578 vcpu->arch.apf.msr_int_val = data; in kvm_pv_enable_async_pf_int()
3580 vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; in kvm_pv_enable_async_pf_int()
3587 kvm_gpc_deactivate(&vcpu->arch.pv_time); in kvmclock_reset()
3588 vcpu->arch.time = 0; in kvmclock_reset()
3593 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_all()
3602 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_guest()
3618 * Flushing all "guest" TLB is always a superset of Hyper-V's fine in kvm_vcpu_flush_tlb_guest()
3627 ++vcpu->stat.tlb_flush; in kvm_vcpu_flush_tlb_current()
3635 * prior before nested VM-Enter/VM-Exit.
3649 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in record_steal_time()
3652 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in record_steal_time()
3656 if (kvm_xen_msr_enabled(vcpu->kvm)) { in record_steal_time()
3661 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in record_steal_time()
3664 if (WARN_ON_ONCE(current->mm != vcpu->kvm->mm)) in record_steal_time()
3667 slots = kvm_memslots(vcpu->kvm); in record_steal_time()
3669 if (unlikely(slots->generation != ghc->generation || in record_steal_time()
3670 gpa != ghc->gpa || in record_steal_time()
3671 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { in record_steal_time()
3673 BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); in record_steal_time()
3675 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || in record_steal_time()
3676 kvm_is_error_hva(ghc->hva) || !ghc->memslot) in record_steal_time()
3680 st = (struct kvm_steal_time __user *)ghc->hva; in record_steal_time()
3687 int err = -EFAULT; in record_steal_time()
3698 "+m" (st->preempted)); in record_steal_time()
3704 vcpu->arch.st.preempted = 0; in record_steal_time()
3706 trace_kvm_pv_tlb_flush(vcpu->vcpu_id, in record_steal_time()
3717 unsafe_put_user(0, &st->preempted, out); in record_steal_time()
3718 vcpu->arch.st.preempted = 0; in record_steal_time()
3721 unsafe_get_user(version, &st->version, out); in record_steal_time()
3726 unsafe_put_user(version, &st->version, out); in record_steal_time()
3730 unsafe_get_user(steal, &st->steal, out); in record_steal_time()
3731 steal += current->sched_info.run_delay - in record_steal_time()
3732 vcpu->arch.st.last_steal; in record_steal_time()
3733 vcpu->arch.st.last_steal = current->sched_info.run_delay; in record_steal_time()
3734 unsafe_put_user(steal, &st->steal, out); in record_steal_time()
3737 unsafe_put_user(version, &st->version, out); in record_steal_time()
3742 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in record_steal_time()
3747 u32 msr = msr_info->index; in kvm_set_msr_common()
3748 u64 data = msr_info->data; in kvm_set_msr_common()
3750 if (msr && msr == vcpu->kvm->arch.xen_hvm_config.msr) in kvm_set_msr_common()
3765 if (msr_info->host_initiated) in kvm_set_msr_common()
3766 vcpu->arch.microcode_version = data; in kvm_set_msr_common()
3769 if (!msr_info->host_initiated || in kvm_set_msr_common()
3772 vcpu->arch.arch_capabilities = data; in kvm_set_msr_common()
3775 if (!msr_info->host_initiated || in kvm_set_msr_common()
3787 if (vcpu->arch.perf_capabilities == data) in kvm_set_msr_common()
3790 vcpu->arch.perf_capabilities = data; in kvm_set_msr_common()
3796 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3824 if (!msr_info->host_initiated && in kvm_set_msr_common()
3851 vcpu->arch.msr_hwcr = data; in kvm_set_msr_common()
3863 vcpu->arch.pat = data; in kvm_set_msr_common()
3869 return kvm_apic_set_base(vcpu, data, msr_info->host_initiated); in kvm_set_msr_common()
3877 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3878 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; in kvm_set_msr_common()
3885 vcpu->arch.ia32_tsc_adjust_msr = data; in kvm_set_msr_common()
3889 u64 old_val = vcpu->arch.ia32_misc_enable_msr; in kvm_set_msr_common()
3891 if (!msr_info->host_initiated) { in kvm_set_msr_common()
3901 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && in kvm_set_msr_common()
3905 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3908 vcpu->arch.ia32_misc_enable_msr = data; in kvm_set_msr_common()
3913 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_set_msr_common()
3915 vcpu->arch.smbase = data; in kvm_set_msr_common()
3918 vcpu->arch.msr_ia32_power_ctl = data; in kvm_set_msr_common()
3921 if (msr_info->host_initiated) { in kvm_set_msr_common()
3924 u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; in kvm_set_msr_common()
3926 vcpu->arch.ia32_tsc_adjust_msr += adj; in kvm_set_msr_common()
3930 if (!msr_info->host_initiated && in kvm_set_msr_common()
3940 vcpu->arch.ia32_xss = data; in kvm_set_msr_common()
3944 if (!msr_info->host_initiated) in kvm_set_msr_common()
3946 vcpu->arch.smi_count = data; in kvm_set_msr_common()
3952 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3953 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3959 vcpu->kvm->arch.wall_clock = data; in kvm_set_msr_common()
3960 kvm_write_wall_clock(vcpu->kvm, data, 0); in kvm_set_msr_common()
3966 kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); in kvm_set_msr_common()
3972 kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); in kvm_set_msr_common()
3992 vcpu->arch.apf.pageready_pending = false; in kvm_set_msr_common()
4006 vcpu->arch.st.msr_val = data; in kvm_set_msr_common()
4027 if (data & (-1ULL << 1)) in kvm_set_msr_common()
4030 vcpu->arch.msr_kvm_poll_control = data; in kvm_set_msr_common()
4035 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4036 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_set_msr_common()
4053 * all pre-dating SVM, but a recommended workaround from in kvm_set_msr_common()
4071 msr_info->host_initiated); in kvm_set_msr_common()
4074 /* Drop writes to this legacy MSR -- see rdmsr in kvm_set_msr_common()
4082 vcpu->arch.osvw.length = data; in kvm_set_msr_common()
4087 vcpu->arch.osvw.status = data; in kvm_set_msr_common()
4090 if (!msr_info->host_initiated) in kvm_set_msr_common()
4092 vcpu->arch.msr_platform_info = data; in kvm_set_msr_common()
4099 vcpu->arch.msr_misc_features_enables = data; in kvm_set_msr_common()
4103 if (!msr_info->host_initiated && in kvm_set_msr_common()
4110 fpu_update_guest_xfd(&vcpu->arch.guest_fpu, data); in kvm_set_msr_common()
4113 if (!msr_info->host_initiated && in kvm_set_msr_common()
4120 vcpu->arch.guest_fpu.xfd_err = data; in kvm_set_msr_common()
4136 u64 mcg_cap = vcpu->arch.mcg_cap; in get_msr_mce()
4146 data = vcpu->arch.mcg_cap; in get_msr_mce()
4151 data = vcpu->arch.mcg_ctl; in get_msr_mce()
4154 data = vcpu->arch.mcg_status; in get_msr_mce()
4156 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4157 last_msr = MSR_IA32_MCx_CTL2(bank_num) - 1; in get_msr_mce()
4163 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL2, in get_msr_mce()
4164 last_msr + 1 - MSR_IA32_MC0_CTL2); in get_msr_mce()
4165 data = vcpu->arch.mci_ctl2_banks[offset]; in get_msr_mce()
4167 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in get_msr_mce()
4168 last_msr = MSR_IA32_MCx_CTL(bank_num) - 1; in get_msr_mce()
4172 offset = array_index_nospec(msr - MSR_IA32_MC0_CTL, in get_msr_mce()
4173 last_msr + 1 - MSR_IA32_MC0_CTL); in get_msr_mce()
4174 data = vcpu->arch.mce_banks[offset]; in get_msr_mce()
4185 switch (msr_info->index) { in kvm_get_msr_common()
4208 * so for existing CPU-specific MSRs. in kvm_get_msr_common()
4215 msr_info->data = 0; in kvm_get_msr_common()
4221 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4223 msr_info->data = 0; in kvm_get_msr_common()
4226 msr_info->data = vcpu->arch.microcode_version; in kvm_get_msr_common()
4231 msr_info->data = vcpu->arch.arch_capabilities; in kvm_get_msr_common()
4236 msr_info->data = vcpu->arch.perf_capabilities; in kvm_get_msr_common()
4239 msr_info->data = vcpu->arch.msr_ia32_power_ctl; in kvm_get_msr_common()
4248 * return L1's TSC value to ensure backwards-compatible in kvm_get_msr_common()
4253 if (msr_info->host_initiated) { in kvm_get_msr_common()
4254 offset = vcpu->arch.l1_tsc_offset; in kvm_get_msr_common()
4255 ratio = vcpu->arch.l1_tsc_scaling_ratio; in kvm_get_msr_common()
4257 offset = vcpu->arch.tsc_offset; in kvm_get_msr_common()
4258 ratio = vcpu->arch.tsc_scaling_ratio; in kvm_get_msr_common()
4261 msr_info->data = kvm_scale_tsc(rdtsc(), ratio) + offset; in kvm_get_msr_common()
4265 msr_info->data = vcpu->arch.pat; in kvm_get_msr_common()
4270 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4272 msr_info->data = 3; in kvm_get_msr_common()
4286 msr_info->data = 1 << 24; in kvm_get_msr_common()
4289 msr_info->data = vcpu->arch.apic_base; in kvm_get_msr_common()
4292 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); in kvm_get_msr_common()
4294 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); in kvm_get_msr_common()
4297 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; in kvm_get_msr_common()
4300 msr_info->data = vcpu->arch.ia32_misc_enable_msr; in kvm_get_msr_common()
4303 if (!IS_ENABLED(CONFIG_KVM_SMM) || !msr_info->host_initiated) in kvm_get_msr_common()
4305 msr_info->data = vcpu->arch.smbase; in kvm_get_msr_common()
4308 msr_info->data = vcpu->arch.smi_count; in kvm_get_msr_common()
4312 msr_info->data = 1000ULL; in kvm_get_msr_common()
4314 msr_info->data |= (((uint64_t)4ULL) << 40); in kvm_get_msr_common()
4317 msr_info->data = vcpu->arch.efer; in kvm_get_msr_common()
4323 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4329 msr_info->data = vcpu->kvm->arch.wall_clock; in kvm_get_msr_common()
4335 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4341 msr_info->data = vcpu->arch.time; in kvm_get_msr_common()
4347 msr_info->data = vcpu->arch.apf.msr_en_val; in kvm_get_msr_common()
4353 msr_info->data = vcpu->arch.apf.msr_int_val; in kvm_get_msr_common()
4359 msr_info->data = 0; in kvm_get_msr_common()
4365 msr_info->data = vcpu->arch.st.msr_val; in kvm_get_msr_common()
4371 msr_info->data = vcpu->arch.pv_eoi.msr_val; in kvm_get_msr_common()
4377 msr_info->data = vcpu->arch.msr_kvm_poll_control; in kvm_get_msr_common()
4384 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4385 case MSR_IA32_MC0_CTL2 ... MSR_IA32_MCx_CTL2(KVM_MAX_MCE_BANKS) - 1: in kvm_get_msr_common()
4386 return get_msr_mce(vcpu, msr_info->index, &msr_info->data, in kvm_get_msr_common()
4387 msr_info->host_initiated); in kvm_get_msr_common()
4389 if (!msr_info->host_initiated && in kvm_get_msr_common()
4392 msr_info->data = vcpu->arch.ia32_xss; in kvm_get_msr_common()
4396 * Provide expected ramp-up count for K7. All other in kvm_get_msr_common()
4404 msr_info->data = 0x20000000; in kvm_get_msr_common()
4418 msr_info->index, &msr_info->data, in kvm_get_msr_common()
4419 msr_info->host_initiated); in kvm_get_msr_common()
4432 msr_info->data = 0xbe702111; in kvm_get_msr_common()
4437 msr_info->data = vcpu->arch.osvw.length; in kvm_get_msr_common()
4442 msr_info->data = vcpu->arch.osvw.status; in kvm_get_msr_common()
4445 if (!msr_info->host_initiated && in kvm_get_msr_common()
4446 !vcpu->kvm->arch.guest_can_read_msr_platform_info) in kvm_get_msr_common()
4448 msr_info->data = vcpu->arch.msr_platform_info; in kvm_get_msr_common()
4451 msr_info->data = vcpu->arch.msr_misc_features_enables; in kvm_get_msr_common()
4454 msr_info->data = vcpu->arch.msr_hwcr; in kvm_get_msr_common()
4458 if (!msr_info->host_initiated && in kvm_get_msr_common()
4462 msr_info->data = vcpu->arch.guest_fpu.fpstate->xfd; in kvm_get_msr_common()
4465 if (!msr_info->host_initiated && in kvm_get_msr_common()
4469 msr_info->data = vcpu->arch.guest_fpu.xfd_err; in kvm_get_msr_common()
4473 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) in kvm_get_msr_common()
4494 for (i = 0; i < msrs->nmsrs; ++i) in __msr_io()
4516 r = -EFAULT; in msr_io()
4520 r = -E2BIG; in msr_io()
4525 entries = memdup_user(user_msrs->entries, size); in msr_io()
4533 if (writeback && copy_to_user(user_msrs->entries, entries, size)) in msr_io()
4534 r = -EFAULT; in msr_io()
4555 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4559 r = kvm_get_hv_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_ioctl_get_supported_hv_cpuid()
4563 r = -EFAULT; in kvm_ioctl_get_supported_hv_cpuid()
4740 r = kvm_x86_ops.nested_ops->get_state ? in kvm_vm_ioctl_check_extension()
4741 kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; in kvm_vm_ioctl_check_extension()
4748 r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; in kvm_vm_ioctl_check_extension()
4793 if (attr->group) { in __kvm_x86_dev_get_attr()
4795 return kvm_x86_call(dev_get_attr)(attr->group, attr->attr, val); in __kvm_x86_dev_get_attr()
4796 return -ENXIO; in __kvm_x86_dev_get_attr()
4799 switch (attr->attr) { in __kvm_x86_dev_get_attr()
4804 return -ENXIO; in __kvm_x86_dev_get_attr()
4810 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_x86_dev_get_attr()
4819 return -EFAULT; in kvm_x86_dev_get_attr()
4843 r = -EFAULT; in kvm_arch_dev_ioctl()
4850 r = -E2BIG; in kvm_arch_dev_ioctl()
4853 r = -EFAULT; in kvm_arch_dev_ioctl()
4854 if (copy_to_user(user_msr_list->indices, &msrs_to_save, in kvm_arch_dev_ioctl()
4857 if (copy_to_user(user_msr_list->indices + num_msrs_to_save, in kvm_arch_dev_ioctl()
4869 r = -EFAULT; in kvm_arch_dev_ioctl()
4873 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, in kvm_arch_dev_ioctl()
4878 r = -EFAULT; in kvm_arch_dev_ioctl()
4885 r = -EFAULT; in kvm_arch_dev_ioctl()
4896 r = -EFAULT; in kvm_arch_dev_ioctl()
4903 r = -E2BIG; in kvm_arch_dev_ioctl()
4906 r = -EFAULT; in kvm_arch_dev_ioctl()
4907 if (copy_to_user(user_msr_list->indices, &msr_based_features, in kvm_arch_dev_ioctl()
4923 r = -EFAULT; in kvm_arch_dev_ioctl()
4931 r = -EFAULT; in kvm_arch_dev_ioctl()
4938 r = -EINVAL; in kvm_arch_dev_ioctl()
4952 return kvm_arch_has_noncoherent_dma(vcpu->kvm); in need_emulate_wbinvd()
4959 vcpu->arch.l1tf_flush_l1d = true; in kvm_arch_vcpu_load()
4961 if (vcpu->scheduled_out && pmu->version && pmu->event_count) { in kvm_arch_vcpu_load()
4962 pmu->need_cleanup = true; in kvm_arch_vcpu_load()
4969 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_load()
4970 else if (vcpu->cpu != -1 && vcpu->cpu != cpu) in kvm_arch_vcpu_load()
4971 smp_call_function_single(vcpu->cpu, in kvm_arch_vcpu_load()
4978 vcpu->arch.host_pkru = read_pkru(); in kvm_arch_vcpu_load()
4981 if (unlikely(vcpu->arch.tsc_offset_adjustment)) { in kvm_arch_vcpu_load()
4982 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); in kvm_arch_vcpu_load()
4983 vcpu->arch.tsc_offset_adjustment = 0; in kvm_arch_vcpu_load()
4987 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { in kvm_arch_vcpu_load()
4988 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : in kvm_arch_vcpu_load()
4989 rdtsc() - vcpu->arch.last_host_tsc; in kvm_arch_vcpu_load()
4995 vcpu->arch.last_guest_tsc); in kvm_arch_vcpu_load()
4997 vcpu->arch.tsc_catchup = 1; in kvm_arch_vcpu_load()
5005 * kvmclock on vcpu->cpu migration in kvm_arch_vcpu_load()
5007 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) in kvm_arch_vcpu_load()
5009 if (vcpu->cpu != cpu) in kvm_arch_vcpu_load()
5011 vcpu->cpu = cpu; in kvm_arch_vcpu_load()
5019 struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache; in kvm_steal_time_set_preempted()
5023 gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; in kvm_steal_time_set_preempted()
5026 * The vCPU can be marked preempted if and only if the VM-Exit was on in kvm_steal_time_set_preempted()
5030 * preempted if and only if the VM-Exit was due to a host interrupt. in kvm_steal_time_set_preempted()
5032 if (!vcpu->arch.at_instruction_boundary) { in kvm_steal_time_set_preempted()
5033 vcpu->stat.preemption_other++; in kvm_steal_time_set_preempted()
5037 vcpu->stat.preemption_reported++; in kvm_steal_time_set_preempted()
5038 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) in kvm_steal_time_set_preempted()
5041 if (vcpu->arch.st.preempted) in kvm_steal_time_set_preempted()
5045 if (unlikely(current->mm != vcpu->kvm->mm)) in kvm_steal_time_set_preempted()
5048 slots = kvm_memslots(vcpu->kvm); in kvm_steal_time_set_preempted()
5050 if (unlikely(slots->generation != ghc->generation || in kvm_steal_time_set_preempted()
5051 gpa != ghc->gpa || in kvm_steal_time_set_preempted()
5052 kvm_is_error_hva(ghc->hva) || !ghc->memslot)) in kvm_steal_time_set_preempted()
5055 st = (struct kvm_steal_time __user *)ghc->hva; in kvm_steal_time_set_preempted()
5056 BUILD_BUG_ON(sizeof(st->preempted) != sizeof(preempted)); in kvm_steal_time_set_preempted()
5058 if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) in kvm_steal_time_set_preempted()
5059 vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; in kvm_steal_time_set_preempted()
5061 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_steal_time_set_preempted()
5068 if (vcpu->preempted) { in kvm_arch_vcpu_put()
5070 * Assume protected guests are in-kernel. Inefficient yielding in kvm_arch_vcpu_put()
5074 vcpu->arch.preempted_in_kernel = vcpu->arch.guest_state_protected || in kvm_arch_vcpu_put()
5081 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_put()
5082 if (kvm_xen_msr_enabled(vcpu->kvm)) in kvm_arch_vcpu_put()
5086 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_put()
5090 vcpu->arch.last_host_tsc = rdtsc(); in kvm_arch_vcpu_put()
5137 * instruction boundary and with no events half-injected. in kvm_vcpu_ready_for_interrupt_injection()
5148 if (irq->irq >= KVM_NR_INTERRUPTS) in kvm_vcpu_ioctl_interrupt()
5149 return -EINVAL; in kvm_vcpu_ioctl_interrupt()
5151 if (!irqchip_in_kernel(vcpu->kvm)) { in kvm_vcpu_ioctl_interrupt()
5152 kvm_queue_interrupt(vcpu, irq->irq, false); in kvm_vcpu_ioctl_interrupt()
5158 * With in-kernel LAPIC, we only use this to inject EXTINT, so in kvm_vcpu_ioctl_interrupt()
5159 * fail for in-kernel 8259. in kvm_vcpu_ioctl_interrupt()
5161 if (pic_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_interrupt()
5162 return -ENXIO; in kvm_vcpu_ioctl_interrupt()
5164 if (vcpu->arch.pending_external_vector != -1) in kvm_vcpu_ioctl_interrupt()
5165 return -EEXIST; in kvm_vcpu_ioctl_interrupt()
5167 vcpu->arch.pending_external_vector = irq->irq; in kvm_vcpu_ioctl_interrupt()
5182 if (tac->flags) in vcpu_ioctl_tpr_access_reporting()
5183 return -EINVAL; in vcpu_ioctl_tpr_access_reporting()
5184 vcpu->arch.tpr_access_reporting = !!tac->enabled; in vcpu_ioctl_tpr_access_reporting()
5194 r = -EINVAL; in kvm_vcpu_ioctl_x86_setup_mce()
5200 vcpu->arch.mcg_cap = mcg_cap; in kvm_vcpu_ioctl_x86_setup_mce()
5203 vcpu->arch.mcg_ctl = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5206 vcpu->arch.mce_banks[bank*4] = ~(u64)0; in kvm_vcpu_ioctl_x86_setup_mce()
5208 vcpu->arch.mci_ctl2_banks[bank] = 0; in kvm_vcpu_ioctl_x86_setup_mce()
5221 * - none of the bits for Machine Check Exceptions are set
5222 * - both the VAL (valid) and UC (uncorrectable) bits are set
5223 * MCI_STATUS_PCC - Processor Context Corrupted
5224 * MCI_STATUS_S - Signaled as a Machine Check Exception
5225 * MCI_STATUS_AR - Software recoverable Action Required
5229 return !mce->mcg_status && in is_ucna()
5230 !(mce->status & (MCI_STATUS_PCC | MCI_STATUS_S | MCI_STATUS_AR)) && in is_ucna()
5231 (mce->status & MCI_STATUS_VAL) && in is_ucna()
5232 (mce->status & MCI_STATUS_UC); in is_ucna()
5237 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_x86_set_ucna()
5239 banks[1] = mce->status; in kvm_vcpu_x86_set_ucna()
5240 banks[2] = mce->addr; in kvm_vcpu_x86_set_ucna()
5241 banks[3] = mce->misc; in kvm_vcpu_x86_set_ucna()
5242 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_x86_set_ucna()
5245 !(vcpu->arch.mci_ctl2_banks[mce->bank] & MCI_CTL2_CMCI_EN)) in kvm_vcpu_x86_set_ucna()
5249 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTCMCI); in kvm_vcpu_x86_set_ucna()
5257 u64 mcg_cap = vcpu->arch.mcg_cap; in kvm_vcpu_ioctl_x86_set_mce()
5259 u64 *banks = vcpu->arch.mce_banks; in kvm_vcpu_ioctl_x86_set_mce()
5261 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) in kvm_vcpu_ioctl_x86_set_mce()
5262 return -EINVAL; in kvm_vcpu_ioctl_x86_set_mce()
5264 banks += array_index_nospec(4 * mce->bank, 4 * bank_num); in kvm_vcpu_ioctl_x86_set_mce()
5273 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && in kvm_vcpu_ioctl_x86_set_mce()
5274 vcpu->arch.mcg_ctl != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5280 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) in kvm_vcpu_ioctl_x86_set_mce()
5282 if (mce->status & MCI_STATUS_UC) { in kvm_vcpu_ioctl_x86_set_mce()
5283 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || in kvm_vcpu_ioctl_x86_set_mce()
5289 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5290 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5291 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5292 vcpu->arch.mcg_status = mce->mcg_status; in kvm_vcpu_ioctl_x86_set_mce()
5293 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5298 mce->status |= MCI_STATUS_OVER; in kvm_vcpu_ioctl_x86_set_mce()
5299 banks[2] = mce->addr; in kvm_vcpu_ioctl_x86_set_mce()
5300 banks[3] = mce->misc; in kvm_vcpu_ioctl_x86_set_mce()
5301 banks[1] = mce->status; in kvm_vcpu_ioctl_x86_set_mce()
5322 * non-exiting _injected_ exception, and a pending exiting exception. in kvm_vcpu_ioctl_x86_get_vcpu_events()
5323 * In that case, ignore the VM-Exiting exception as it's an extension in kvm_vcpu_ioctl_x86_get_vcpu_events()
5326 if (vcpu->arch.exception_vmexit.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5327 !vcpu->arch.exception.pending && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5328 !vcpu->arch.exception.injected) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5329 ex = &vcpu->arch.exception_vmexit; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5331 ex = &vcpu->arch.exception; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5336 * intercepts #PF, ditto for DR6 and #DBs. If the per-VM capability, in kvm_vcpu_ioctl_x86_get_vcpu_events()
5341 if (!vcpu->kvm->arch.exception_payload_enabled && in kvm_vcpu_ioctl_x86_get_vcpu_events()
5342 ex->pending && ex->has_payload) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5353 if (!kvm_exception_is_soft(ex->vector)) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5354 events->exception.injected = ex->injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5355 events->exception.pending = ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5361 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5362 events->exception.injected |= ex->pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5364 events->exception.nr = ex->vector; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5365 events->exception.has_error_code = ex->has_error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5366 events->exception.error_code = ex->error_code; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5367 events->exception_has_payload = ex->has_payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5368 events->exception_payload = ex->payload; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5370 events->interrupt.injected = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5371 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5372 events->interrupt.nr = vcpu->arch.interrupt.nr; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5373 events->interrupt.shadow = kvm_x86_call(get_interrupt_shadow)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5375 events->nmi.injected = vcpu->arch.nmi_injected; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5376 events->nmi.pending = kvm_get_nr_pending_nmis(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5377 events->nmi.masked = kvm_x86_call(get_nmi_mask)(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5379 /* events->sipi_vector is never valid when reporting to user space */ in kvm_vcpu_ioctl_x86_get_vcpu_events()
5382 events->smi.smm = is_smm(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5383 events->smi.pending = vcpu->arch.smi_pending; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5384 events->smi.smm_inside_nmi = in kvm_vcpu_ioctl_x86_get_vcpu_events()
5385 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5387 events->smi.latched_init = kvm_lapic_latched_init(vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5389 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_get_vcpu_events()
5392 if (vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_get_vcpu_events()
5393 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5394 if (vcpu->kvm->arch.triple_fault_event) { in kvm_vcpu_ioctl_x86_get_vcpu_events()
5395 events->triple_fault.pending = kvm_test_request(KVM_REQ_TRIPLE_FAULT, vcpu); in kvm_vcpu_ioctl_x86_get_vcpu_events()
5396 events->flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT; in kvm_vcpu_ioctl_x86_get_vcpu_events()
5403 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING in kvm_vcpu_ioctl_x86_set_vcpu_events()
5409 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5411 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5412 if (!vcpu->kvm->arch.exception_payload_enabled) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5413 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5414 if (events->exception.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5415 events->exception.injected = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5417 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5419 events->exception.pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5420 events->exception_has_payload = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5423 if ((events->exception.injected || events->exception.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5424 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5425 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5428 if (events->flags & KVM_VCPUEVENT_VALID_SMM && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5429 (events->smi.smm || events->smi.pending) && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5430 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5431 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5437 * morph the exception to a VM-Exit if appropriate. Do this only for in kvm_vcpu_ioctl_x86_set_vcpu_events()
5438 * pending exceptions, already-injected exceptions are not subject to in kvm_vcpu_ioctl_x86_set_vcpu_events()
5441 * pending exception, which in turn may cause a spurious VM-Exit. in kvm_vcpu_ioctl_x86_set_vcpu_events()
5443 vcpu->arch.exception_from_userspace = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5445 vcpu->arch.exception_vmexit.pending = false; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5447 vcpu->arch.exception.injected = events->exception.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5448 vcpu->arch.exception.pending = events->exception.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5449 vcpu->arch.exception.vector = events->exception.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5450 vcpu->arch.exception.has_error_code = events->exception.has_error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5451 vcpu->arch.exception.error_code = events->exception.error_code; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5452 vcpu->arch.exception.has_payload = events->exception_has_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5453 vcpu->arch.exception.payload = events->exception_payload; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5455 vcpu->arch.interrupt.injected = events->interrupt.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5456 vcpu->arch.interrupt.nr = events->interrupt.nr; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5457 vcpu->arch.interrupt.soft = events->interrupt.soft; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5458 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5460 events->interrupt.shadow); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5462 vcpu->arch.nmi_injected = events->nmi.injected; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5463 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5464 vcpu->arch.nmi_pending = 0; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5465 atomic_set(&vcpu->arch.nmi_queued, events->nmi.pending); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5466 if (events->nmi.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5469 kvm_x86_call(set_nmi_mask)(vcpu, events->nmi.masked); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5471 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && in kvm_vcpu_ioctl_x86_set_vcpu_events()
5473 vcpu->arch.apic->sipi_vector = events->sipi_vector; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5475 if (events->flags & KVM_VCPUEVENT_VALID_SMM) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5477 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5479 kvm_smm_changed(vcpu, events->smi.smm); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5482 vcpu->arch.smi_pending = events->smi.pending; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5484 if (events->smi.smm) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5485 if (events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5486 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5488 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5492 if (events->smi.smm || events->smi.pending || in kvm_vcpu_ioctl_x86_set_vcpu_events()
5493 events->smi.smm_inside_nmi) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5494 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5498 if (events->smi.latched_init) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5499 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5501 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); in kvm_vcpu_ioctl_x86_set_vcpu_events()
5505 if (events->flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT) { in kvm_vcpu_ioctl_x86_set_vcpu_events()
5506 if (!vcpu->kvm->arch.triple_fault_event) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5507 return -EINVAL; in kvm_vcpu_ioctl_x86_set_vcpu_events()
5508 if (events->triple_fault.pending) in kvm_vcpu_ioctl_x86_set_vcpu_events()
5524 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_debugregs()
5525 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_debugregs()
5526 return -EINVAL; in kvm_vcpu_ioctl_x86_get_debugregs()
5530 BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.db) != ARRAY_SIZE(dbgregs->db)); in kvm_vcpu_ioctl_x86_get_debugregs()
5531 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_get_debugregs()
5532 dbgregs->db[i] = vcpu->arch.db[i]; in kvm_vcpu_ioctl_x86_get_debugregs()
5534 dbgregs->dr6 = vcpu->arch.dr6; in kvm_vcpu_ioctl_x86_get_debugregs()
5535 dbgregs->dr7 = vcpu->arch.dr7; in kvm_vcpu_ioctl_x86_get_debugregs()
5544 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_debugregs()
5545 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_debugregs()
5546 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5548 if (dbgregs->flags) in kvm_vcpu_ioctl_x86_set_debugregs()
5549 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5551 if (!kvm_dr6_valid(dbgregs->dr6)) in kvm_vcpu_ioctl_x86_set_debugregs()
5552 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5553 if (!kvm_dr7_valid(dbgregs->dr7)) in kvm_vcpu_ioctl_x86_set_debugregs()
5554 return -EINVAL; in kvm_vcpu_ioctl_x86_set_debugregs()
5556 for (i = 0; i < ARRAY_SIZE(vcpu->arch.db); i++) in kvm_vcpu_ioctl_x86_set_debugregs()
5557 vcpu->arch.db[i] = dbgregs->db[i]; in kvm_vcpu_ioctl_x86_set_debugregs()
5560 vcpu->arch.dr6 = dbgregs->dr6; in kvm_vcpu_ioctl_x86_set_debugregs()
5561 vcpu->arch.dr7 = dbgregs->dr7; in kvm_vcpu_ioctl_x86_set_debugregs()
5583 u64 supported_xcr0 = vcpu->arch.guest_supported_xcr0 | in kvm_vcpu_ioctl_x86_get_xsave2()
5586 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_get_xsave2()
5587 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_get_xsave2()
5589 fpu_copy_guest_fpstate_to_uabi(&vcpu->arch.guest_fpu, state, size, in kvm_vcpu_ioctl_x86_get_xsave2()
5590 supported_xcr0, vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_get_xsave2()
5597 return kvm_vcpu_ioctl_x86_get_xsave2(vcpu, (void *)guest_xsave->region, in kvm_vcpu_ioctl_x86_get_xsave()
5598 sizeof(guest_xsave->region)); in kvm_vcpu_ioctl_x86_get_xsave()
5604 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_vcpu_ioctl_x86_set_xsave()
5605 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_vcpu_ioctl_x86_set_xsave()
5607 return fpu_copy_uabi_to_guest_fpstate(&vcpu->arch.guest_fpu, in kvm_vcpu_ioctl_x86_set_xsave()
5608 guest_xsave->region, in kvm_vcpu_ioctl_x86_set_xsave()
5610 &vcpu->arch.pkru); in kvm_vcpu_ioctl_x86_set_xsave()
5616 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_get_xcrs()
5617 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_get_xcrs()
5618 return -EINVAL; in kvm_vcpu_ioctl_x86_get_xcrs()
5621 guest_xcrs->nr_xcrs = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5625 guest_xcrs->nr_xcrs = 1; in kvm_vcpu_ioctl_x86_get_xcrs()
5626 guest_xcrs->flags = 0; in kvm_vcpu_ioctl_x86_get_xcrs()
5627 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; in kvm_vcpu_ioctl_x86_get_xcrs()
5628 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; in kvm_vcpu_ioctl_x86_get_xcrs()
5637 if (vcpu->kvm->arch.has_protected_state && in kvm_vcpu_ioctl_x86_set_xcrs()
5638 vcpu->arch.guest_state_protected) in kvm_vcpu_ioctl_x86_set_xcrs()
5639 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5642 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5644 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) in kvm_vcpu_ioctl_x86_set_xcrs()
5645 return -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5647 for (i = 0; i < guest_xcrs->nr_xcrs; i++) in kvm_vcpu_ioctl_x86_set_xcrs()
5649 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { in kvm_vcpu_ioctl_x86_set_xcrs()
5651 guest_xcrs->xcrs[i].value); in kvm_vcpu_ioctl_x86_set_xcrs()
5655 r = -EINVAL; in kvm_vcpu_ioctl_x86_set_xcrs()
5667 if (!vcpu->arch.pv_time.active) in kvm_set_guest_paused()
5668 return -EINVAL; in kvm_set_guest_paused()
5669 vcpu->arch.pvclock_set_guest_stopped_request = true; in kvm_set_guest_paused()
5679 switch (attr->attr) { in kvm_arch_tsc_has_attr()
5684 r = -ENXIO; in kvm_arch_tsc_has_attr()
5693 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_get_attr()
5696 switch (attr->attr) { in kvm_arch_tsc_get_attr()
5698 r = -EFAULT; in kvm_arch_tsc_get_attr()
5699 if (put_user(vcpu->arch.l1_tsc_offset, uaddr)) in kvm_arch_tsc_get_attr()
5704 r = -ENXIO; in kvm_arch_tsc_get_attr()
5713 u64 __user *uaddr = u64_to_user_ptr(attr->addr); in kvm_arch_tsc_set_attr()
5714 struct kvm *kvm = vcpu->kvm; in kvm_arch_tsc_set_attr()
5717 switch (attr->attr) { in kvm_arch_tsc_set_attr()
5723 r = -EFAULT; in kvm_arch_tsc_set_attr()
5727 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5729 matched = (vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5730 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && in kvm_arch_tsc_set_attr()
5731 kvm->arch.last_tsc_offset == offset); in kvm_arch_tsc_set_attr()
5733 tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset; in kvm_arch_tsc_set_attr()
5736 kvm->arch.user_set_tsc = true; in kvm_arch_tsc_set_attr()
5738 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_tsc_set_attr()
5744 r = -ENXIO; in kvm_arch_tsc_set_attr()
5758 return -EFAULT; in kvm_vcpu_ioctl_device_attr()
5761 return -ENXIO; in kvm_vcpu_ioctl_device_attr()
5781 if (cap->flags) in kvm_vcpu_ioctl_enable_cap()
5782 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5784 switch (cap->cap) { in kvm_vcpu_ioctl_enable_cap()
5787 if (cap->args[0]) in kvm_vcpu_ioctl_enable_cap()
5788 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5792 if (!irqchip_in_kernel(vcpu->kvm)) in kvm_vcpu_ioctl_enable_cap()
5793 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5794 return kvm_hv_activate_synic(vcpu, cap->cap == in kvm_vcpu_ioctl_enable_cap()
5802 if (!kvm_x86_ops.nested_ops->enable_evmcs) in kvm_vcpu_ioctl_enable_cap()
5803 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5804 r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); in kvm_vcpu_ioctl_enable_cap()
5806 user_ptr = (void __user *)(uintptr_t)cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5809 r = -EFAULT; in kvm_vcpu_ioctl_enable_cap()
5815 return -ENOTTY; in kvm_vcpu_ioctl_enable_cap()
5820 return kvm_hv_set_enforce_cpuid(vcpu, cap->args[0]); in kvm_vcpu_ioctl_enable_cap()
5824 vcpu->arch.pv_cpuid.enforce = cap->args[0]; in kvm_vcpu_ioctl_enable_cap()
5825 if (vcpu->arch.pv_cpuid.enforce) in kvm_vcpu_ioctl_enable_cap()
5830 return -EINVAL; in kvm_vcpu_ioctl_enable_cap()
5837 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl()
5853 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5858 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
5864 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5871 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5886 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5904 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5907 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5914 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5918 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5925 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5929 cpuid_arg->entries); in kvm_arch_vcpu_ioctl()
5932 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5939 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5941 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5945 int idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5947 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5953 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5959 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5969 r = -EINVAL; in kvm_arch_vcpu_ioctl()
5972 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5975 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
5977 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
5983 r = -EFAULT; in kvm_arch_vcpu_ioctl()
5992 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6003 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6012 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6028 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6038 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6047 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6048 if (vcpu->arch.guest_fpu.uabi_size > sizeof(struct kvm_xsave)) in kvm_arch_vcpu_ioctl()
6052 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6060 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6067 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6080 int size = vcpu->arch.guest_fpu.uabi_size; in kvm_arch_vcpu_ioctl()
6083 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6091 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6101 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6109 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6129 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6145 r = vcpu->arch.virtual_tsc_khz; in kvm_arch_vcpu_ioctl()
6155 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6165 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6166 if (!kvm_x86_ops.nested_ops->get_state) in kvm_arch_vcpu_ioctl()
6169 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); in kvm_arch_vcpu_ioctl()
6170 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6171 if (get_user(user_data_size, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6174 r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, in kvm_arch_vcpu_ioctl()
6180 if (put_user(r, &user_kvm_nested_state->size)) in kvm_arch_vcpu_ioctl()
6181 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6183 r = -E2BIG; in kvm_arch_vcpu_ioctl()
6195 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6196 if (!kvm_x86_ops.nested_ops->set_state) in kvm_arch_vcpu_ioctl()
6199 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6203 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6218 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl()
6219 r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); in kvm_arch_vcpu_ioctl()
6220 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl()
6232 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6237 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6243 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6251 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6252 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6253 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6257 r = -ENOMEM; in kvm_arch_vcpu_ioctl()
6261 r = -EFAULT; in kvm_arch_vcpu_ioctl()
6268 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6269 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl()
6270 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl()
6288 r = -EINVAL; in kvm_arch_vcpu_ioctl()
6306 if (addr > (unsigned int)(-3 * PAGE_SIZE)) in kvm_vm_ioctl_set_tss_addr()
6307 return -EINVAL; in kvm_vm_ioctl_set_tss_addr()
6322 return -EINVAL; in kvm_vm_ioctl_set_nr_mmu_pages()
6324 mutex_lock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6327 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; in kvm_vm_ioctl_set_nr_mmu_pages()
6329 mutex_unlock(&kvm->slots_lock); in kvm_vm_ioctl_set_nr_mmu_pages()
6335 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_get_irqchip()
6339 switch (chip->chip_id) { in kvm_vm_ioctl_get_irqchip()
6341 memcpy(&chip->chip.pic, &pic->pics[0], in kvm_vm_ioctl_get_irqchip()
6345 memcpy(&chip->chip.pic, &pic->pics[1], in kvm_vm_ioctl_get_irqchip()
6349 kvm_get_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_get_irqchip()
6352 r = -EINVAL; in kvm_vm_ioctl_get_irqchip()
6360 struct kvm_pic *pic = kvm->arch.vpic; in kvm_vm_ioctl_set_irqchip()
6364 switch (chip->chip_id) { in kvm_vm_ioctl_set_irqchip()
6366 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6367 memcpy(&pic->pics[0], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6369 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6372 spin_lock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6373 memcpy(&pic->pics[1], &chip->chip.pic, in kvm_vm_ioctl_set_irqchip()
6375 spin_unlock(&pic->lock); in kvm_vm_ioctl_set_irqchip()
6378 kvm_set_ioapic(kvm, &chip->chip.ioapic); in kvm_vm_ioctl_set_irqchip()
6381 r = -EINVAL; in kvm_vm_ioctl_set_irqchip()
6390 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; in kvm_vm_ioctl_get_pit()
6392 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); in kvm_vm_ioctl_get_pit()
6394 mutex_lock(&kps->lock); in kvm_vm_ioctl_get_pit()
6395 memcpy(ps, &kps->channels, sizeof(*ps)); in kvm_vm_ioctl_get_pit()
6396 mutex_unlock(&kps->lock); in kvm_vm_ioctl_get_pit()
6403 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit()
6405 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6406 memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); in kvm_vm_ioctl_set_pit()
6408 kvm_pit_load_count(pit, i, ps->channels[i].count, 0); in kvm_vm_ioctl_set_pit()
6409 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit()
6415 mutex_lock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6416 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, in kvm_vm_ioctl_get_pit2()
6417 sizeof(ps->channels)); in kvm_vm_ioctl_get_pit2()
6418 ps->flags = kvm->arch.vpit->pit_state.flags; in kvm_vm_ioctl_get_pit2()
6419 mutex_unlock(&kvm->arch.vpit->pit_state.lock); in kvm_vm_ioctl_get_pit2()
6420 memset(&ps->reserved, 0, sizeof(ps->reserved)); in kvm_vm_ioctl_get_pit2()
6429 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_set_pit2()
6431 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6432 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6433 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; in kvm_vm_ioctl_set_pit2()
6436 memcpy(&pit->pit_state.channels, &ps->channels, in kvm_vm_ioctl_set_pit2()
6437 sizeof(pit->pit_state.channels)); in kvm_vm_ioctl_set_pit2()
6438 pit->pit_state.flags = ps->flags; in kvm_vm_ioctl_set_pit2()
6440 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, in kvm_vm_ioctl_set_pit2()
6442 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_set_pit2()
6449 struct kvm_pit *pit = kvm->arch.vpit; in kvm_vm_ioctl_reinject()
6451 /* pit->pit_state.lock was overloaded to prevent userspace from getting in kvm_vm_ioctl_reinject()
6455 mutex_lock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6456 kvm_pit_set_reinject(pit, control->pit_reinject); in kvm_vm_ioctl_reinject()
6457 mutex_unlock(&pit->pit_state.lock); in kvm_vm_ioctl_reinject()
6468 * on all VM-Exits, thus we only need to kick running vCPUs to force a in kvm_arch_sync_dirty_log()
6469 * VM-Exit. in kvm_arch_sync_dirty_log()
6485 return -ENXIO; in kvm_vm_ioctl_irq_line()
6487 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, in kvm_vm_ioctl_irq_line()
6488 irq_event->irq, irq_event->level, in kvm_vm_ioctl_irq_line()
6498 if (cap->flags) in kvm_vm_ioctl_enable_cap()
6499 return -EINVAL; in kvm_vm_ioctl_enable_cap()
6501 switch (cap->cap) { in kvm_vm_ioctl_enable_cap()
6503 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6504 if (cap->args[0] & ~KVM_X86_VALID_QUIRKS) in kvm_vm_ioctl_enable_cap()
6508 kvm->arch.disabled_quirks = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6512 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6513 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6514 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) in kvm_vm_ioctl_enable_cap()
6516 r = -EEXIST; in kvm_vm_ioctl_enable_cap()
6519 if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6523 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; in kvm_vm_ioctl_enable_cap()
6524 kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6528 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6532 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6533 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) in kvm_vm_ioctl_enable_cap()
6536 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) in kvm_vm_ioctl_enable_cap()
6537 kvm->arch.x2apic_format = true; in kvm_vm_ioctl_enable_cap()
6538 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) in kvm_vm_ioctl_enable_cap()
6539 kvm->arch.x2apic_broadcast_quirk_disabled = true; in kvm_vm_ioctl_enable_cap()
6544 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6545 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) in kvm_vm_ioctl_enable_cap()
6548 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) in kvm_vm_ioctl_enable_cap()
6549 kvm->arch.pause_in_guest = true; in kvm_vm_ioctl_enable_cap()
6551 #define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerabilit… in kvm_vm_ioctl_enable_cap()
6556 (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) in kvm_vm_ioctl_enable_cap()
6559 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && in kvm_vm_ioctl_enable_cap()
6561 kvm->arch.mwait_in_guest = true; in kvm_vm_ioctl_enable_cap()
6562 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) in kvm_vm_ioctl_enable_cap()
6563 kvm->arch.hlt_in_guest = true; in kvm_vm_ioctl_enable_cap()
6564 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) in kvm_vm_ioctl_enable_cap()
6565 kvm->arch.cstate_in_guest = true; in kvm_vm_ioctl_enable_cap()
6571 kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6575 kvm->arch.exception_payload_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6579 kvm->arch.triple_fault_event = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6583 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6584 if (cap->args[0] & ~KVM_MSR_EXIT_REASON_VALID_MASK) in kvm_vm_ioctl_enable_cap()
6586 kvm->arch.user_space_msr_mask = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6590 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6591 if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE) in kvm_vm_ioctl_enable_cap()
6594 if ((cap->args[0] & KVM_BUS_LOCK_DETECTION_OFF) && in kvm_vm_ioctl_enable_cap()
6595 (cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT)) in kvm_vm_ioctl_enable_cap()
6599 cap->args[0] & KVM_BUS_LOCK_DETECTION_EXIT) in kvm_vm_ioctl_enable_cap()
6600 kvm->arch.bus_lock_detection_enabled = true; in kvm_vm_ioctl_enable_cap()
6607 r = sgx_set_attribute(&allowed_attributes, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6614 kvm->arch.sgx_provisioning_allowed = true; in kvm_vm_ioctl_enable_cap()
6616 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6621 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6625 r = kvm_x86_call(vm_copy_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6628 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6632 r = kvm_x86_call(vm_move_enc_context_from)(kvm, cap->args[0]); in kvm_vm_ioctl_enable_cap()
6635 if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { in kvm_vm_ioctl_enable_cap()
6636 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6639 kvm->arch.hypercall_exit_enabled = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6643 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6644 if (cap->args[0] & ~1) in kvm_vm_ioctl_enable_cap()
6646 kvm->arch.exit_on_emulation_error = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6650 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6651 if (!enable_pmu || (cap->args[0] & ~KVM_CAP_PMU_VALID_MASK)) in kvm_vm_ioctl_enable_cap()
6654 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6655 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6656 kvm->arch.enable_pmu = !(cap->args[0] & KVM_PMU_CAP_DISABLE); in kvm_vm_ioctl_enable_cap()
6659 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6662 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6663 if (cap->args[0] > KVM_MAX_VCPU_IDS) in kvm_vm_ioctl_enable_cap()
6666 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6667 if (kvm->arch.bsp_vcpu_id > cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6669 } else if (kvm->arch.max_vcpu_ids == cap->args[0]) { in kvm_vm_ioctl_enable_cap()
6671 } else if (!kvm->arch.max_vcpu_ids) { in kvm_vm_ioctl_enable_cap()
6672 kvm->arch.max_vcpu_ids = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6675 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6678 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6679 if ((u32)cap->args[0] & ~KVM_X86_NOTIFY_VMEXIT_VALID_BITS) in kvm_vm_ioctl_enable_cap()
6683 if (!((u32)cap->args[0] & KVM_X86_NOTIFY_VMEXIT_ENABLED)) in kvm_vm_ioctl_enable_cap()
6685 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6686 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6687 kvm->arch.notify_window = cap->args[0] >> 32; in kvm_vm_ioctl_enable_cap()
6688 kvm->arch.notify_vmexit_flags = (u32)cap->args[0]; in kvm_vm_ioctl_enable_cap()
6691 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6694 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6708 r = -EPERM; in kvm_vm_ioctl_enable_cap()
6712 if (cap->args[0]) in kvm_vm_ioctl_enable_cap()
6715 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6716 if (!kvm->created_vcpus) { in kvm_vm_ioctl_enable_cap()
6717 kvm->arch.disable_nx_huge_pages = true; in kvm_vm_ioctl_enable_cap()
6720 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6723 u64 bus_cycle_ns = cap->args[0]; in kvm_vm_ioctl_enable_cap()
6730 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6736 mutex_lock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6738 r = -ENXIO; in kvm_vm_ioctl_enable_cap()
6739 else if (kvm->created_vcpus) in kvm_vm_ioctl_enable_cap()
6740 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6742 kvm->arch.apic_bus_cycle_ns = bus_cycle_ns; in kvm_vm_ioctl_enable_cap()
6743 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_enable_cap()
6747 r = -EINVAL; in kvm_vm_ioctl_enable_cap()
6761 msr_filter->default_allow = default_allow; in kvm_alloc_msr_filter()
6772 for (i = 0; i < msr_filter->count; i++) in kvm_free_msr_filter()
6773 kfree(msr_filter->ranges[i].bitmap); in kvm_free_msr_filter()
6784 if (!user_range->nmsrs) in kvm_add_msr_filter()
6787 if (user_range->flags & ~KVM_MSR_FILTER_RANGE_VALID_MASK) in kvm_add_msr_filter()
6788 return -EINVAL; in kvm_add_msr_filter()
6790 if (!user_range->flags) in kvm_add_msr_filter()
6791 return -EINVAL; in kvm_add_msr_filter()
6793 bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); in kvm_add_msr_filter()
6795 return -EINVAL; in kvm_add_msr_filter()
6797 bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); in kvm_add_msr_filter()
6801 msr_filter->ranges[msr_filter->count] = (struct msr_bitmap_range) { in kvm_add_msr_filter()
6802 .flags = user_range->flags, in kvm_add_msr_filter()
6803 .base = user_range->base, in kvm_add_msr_filter()
6804 .nmsrs = user_range->nmsrs, in kvm_add_msr_filter()
6808 msr_filter->count++; in kvm_add_msr_filter()
6821 if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK) in kvm_vm_ioctl_set_msr_filter()
6822 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6824 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) in kvm_vm_ioctl_set_msr_filter()
6825 empty &= !filter->ranges[i].nmsrs; in kvm_vm_ioctl_set_msr_filter()
6827 default_allow = !(filter->flags & KVM_MSR_FILTER_DEFAULT_DENY); in kvm_vm_ioctl_set_msr_filter()
6829 return -EINVAL; in kvm_vm_ioctl_set_msr_filter()
6833 return -ENOMEM; in kvm_vm_ioctl_set_msr_filter()
6835 for (i = 0; i < ARRAY_SIZE(filter->ranges); i++) { in kvm_vm_ioctl_set_msr_filter()
6836 r = kvm_add_msr_filter(new_filter, &filter->ranges[i]); in kvm_vm_ioctl_set_msr_filter()
6843 mutex_lock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6844 old_filter = rcu_replace_pointer(kvm->arch.msr_filter, new_filter, in kvm_vm_ioctl_set_msr_filter()
6845 mutex_is_locked(&kvm->lock)); in kvm_vm_ioctl_set_msr_filter()
6846 mutex_unlock(&kvm->lock); in kvm_vm_ioctl_set_msr_filter()
6847 synchronize_srcu(&kvm->srcu); in kvm_vm_ioctl_set_msr_filter()
6876 struct kvm *kvm = filp->private_data; in kvm_arch_vm_compat_ioctl()
6877 long r = -ENOTTY; in kvm_arch_vm_compat_ioctl()
6888 return -EFAULT; in kvm_arch_vm_compat_ioctl()
6896 .flags = cr->flags, in kvm_arch_vm_compat_ioctl()
6897 .nmsrs = cr->nmsrs, in kvm_arch_vm_compat_ioctl()
6898 .base = cr->base, in kvm_arch_vm_compat_ioctl()
6899 .bitmap = (__u8 *)(ulong)cr->bitmap, in kvm_arch_vm_compat_ioctl()
6919 mutex_lock(&kvm->lock); in kvm_arch_suspend_notifier()
6921 if (!vcpu->arch.pv_time.active) in kvm_arch_suspend_notifier()
6927 vcpu->vcpu_id, ret); in kvm_arch_suspend_notifier()
6931 mutex_unlock(&kvm->lock); in kvm_arch_suspend_notifier()
6954 return -EFAULT; in kvm_vm_ioctl_get_clock()
6961 struct kvm_arch *ka = &kvm->arch; in kvm_vm_ioctl_set_clock()
6966 return -EFAULT; in kvm_vm_ioctl_set_clock()
6973 return -EINVAL; in kvm_vm_ioctl_set_clock()
6993 data.clock += now_real_ns - data.realtime; in kvm_vm_ioctl_set_clock()
6996 if (ka->use_master_clock) in kvm_vm_ioctl_set_clock()
6997 now_raw_ns = ka->master_kernel_ns; in kvm_vm_ioctl_set_clock()
7000 ka->kvmclock_offset = data.clock - now_raw_ns; in kvm_vm_ioctl_set_clock()
7007 struct kvm *kvm = filp->private_data; in kvm_arch_vm_ioctl()
7009 int r = -ENOTTY; in kvm_arch_vm_ioctl()
7011 * This union makes it completely explicit to gcc-3.x in kvm_arch_vm_ioctl()
7028 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7029 r = -EINVAL; in kvm_arch_vm_ioctl()
7030 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7032 r = -EFAULT; in kvm_arch_vm_ioctl()
7037 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7044 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7046 r = -EEXIST; in kvm_arch_vm_ioctl()
7050 r = -EINVAL; in kvm_arch_vm_ioctl()
7051 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7070 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ in kvm_arch_vm_ioctl()
7072 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; in kvm_arch_vm_ioctl()
7075 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7082 r = -EFAULT; in kvm_arch_vm_ioctl()
7087 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7088 r = -EEXIST; in kvm_arch_vm_ioctl()
7089 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7091 r = -ENOENT; in kvm_arch_vm_ioctl()
7094 r = -ENOMEM; in kvm_arch_vm_ioctl()
7095 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); in kvm_arch_vm_ioctl()
7096 if (kvm->arch.vpit) in kvm_arch_vm_ioctl()
7099 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7111 r = -ENXIO; in kvm_arch_vm_ioctl()
7117 r = -EFAULT; in kvm_arch_vm_ioctl()
7135 r = -ENXIO; in kvm_arch_vm_ioctl()
7144 r = -EFAULT; in kvm_arch_vm_ioctl()
7147 r = -ENXIO; in kvm_arch_vm_ioctl()
7148 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7153 r = -EFAULT; in kvm_arch_vm_ioctl()
7160 r = -EFAULT; in kvm_arch_vm_ioctl()
7163 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7164 r = -ENXIO; in kvm_arch_vm_ioctl()
7165 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7169 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7173 r = -ENXIO; in kvm_arch_vm_ioctl()
7174 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7179 r = -EFAULT; in kvm_arch_vm_ioctl()
7186 r = -EFAULT; in kvm_arch_vm_ioctl()
7189 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7190 r = -ENXIO; in kvm_arch_vm_ioctl()
7191 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7195 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7200 r = -EFAULT; in kvm_arch_vm_ioctl()
7203 r = -ENXIO; in kvm_arch_vm_ioctl()
7204 if (!kvm->arch.vpit) in kvm_arch_vm_ioctl()
7211 mutex_lock(&kvm->lock); in kvm_arch_vm_ioctl()
7212 if (kvm->created_vcpus) in kvm_arch_vm_ioctl()
7213 r = -EBUSY; in kvm_arch_vm_ioctl()
7215 (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) in kvm_arch_vm_ioctl()
7216 r = -EINVAL; in kvm_arch_vm_ioctl()
7218 kvm->arch.bsp_vcpu_id = arg; in kvm_arch_vm_ioctl()
7219 mutex_unlock(&kvm->lock); in kvm_arch_vm_ioctl()
7224 r = -EFAULT; in kvm_arch_vm_ioctl()
7233 r = -EFAULT; in kvm_arch_vm_ioctl()
7238 r = -EFAULT; in kvm_arch_vm_ioctl()
7244 r = -EFAULT; in kvm_arch_vm_ioctl()
7253 r = -EFAULT; in kvm_arch_vm_ioctl()
7269 r = -EINVAL; in kvm_arch_vm_ioctl()
7279 WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz); in kvm_arch_vm_ioctl()
7285 r = READ_ONCE(kvm->arch.default_tsc_khz); in kvm_arch_vm_ioctl()
7289 r = -ENOTTY; in kvm_arch_vm_ioctl()
7299 r = -EFAULT; in kvm_arch_vm_ioctl()
7303 r = -ENOTTY; in kvm_arch_vm_ioctl()
7313 r = -EFAULT; in kvm_arch_vm_ioctl()
7317 r = -ENOTTY; in kvm_arch_vm_ioctl()
7328 r = -EFAULT; in kvm_arch_vm_ioctl()
7343 return -EFAULT; in kvm_arch_vm_ioctl()
7349 r = -ENOTTY; in kvm_arch_vm_ioctl()
7409 (msr_index - MSR_IA32_RTIT_ADDR0_A >= in kvm_probe_msr_to_save()
7414 MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7415 if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >= in kvm_probe_msr_to_save()
7420 MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1: in kvm_probe_msr_to_save()
7421 if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >= in kvm_probe_msr_to_save()
7426 MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1: in kvm_probe_msr_to_save()
7427 if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >= in kvm_probe_msr_to_save()
7496 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) in vcpu_mmio_write()
7501 len -= n; in vcpu_mmio_write()
7516 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, in vcpu_mmio_read()
7523 len -= n; in vcpu_mmio_read()
7531 struct kvm_segment *var, int seg) in kvm_set_segment() argument
7533 kvm_x86_call(set_segment)(vcpu, var, seg); in kvm_set_segment()
7537 struct kvm_segment *var, int seg) in kvm_get_segment() argument
7539 kvm_x86_call(get_segment)(vcpu, var, seg); in kvm_get_segment()
7545 struct kvm_mmu *mmu = vcpu->arch.mmu; in translate_nested_gpa()
7550 /* NPT walks are always user-walks */ in translate_nested_gpa()
7552 t_gpa = mmu->gva_to_gpa(vcpu, mmu, gpa, access, exception); in translate_nested_gpa()
7560 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_read()
7563 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_read()
7570 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_write()
7574 return mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in kvm_mmu_gva_to_gpa_write()
7582 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_mmu_gva_to_gpa_system()
7584 return mmu->gva_to_gpa(vcpu, mmu, gva, 0, exception); in kvm_mmu_gva_to_gpa_system()
7591 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_read_guest_virt_helper()
7596 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_read_guest_virt_helper()
7597 unsigned offset = addr & (PAGE_SIZE-1); in kvm_read_guest_virt_helper()
7598 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_read_guest_virt_helper()
7610 bytes -= toread; in kvm_read_guest_virt_helper()
7624 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fetch_guest_virt()
7630 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access|PFERR_FETCH_MASK, in kvm_fetch_guest_virt()
7635 offset = addr & (PAGE_SIZE-1); in kvm_fetch_guest_virt()
7637 bytes = (unsigned)PAGE_SIZE - offset; in kvm_fetch_guest_virt()
7683 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_write_guest_virt_helper()
7688 gpa_t gpa = mmu->gva_to_gpa(vcpu, mmu, addr, access, exception); in kvm_write_guest_virt_helper()
7689 unsigned offset = addr & (PAGE_SIZE-1); in kvm_write_guest_virt_helper()
7690 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); in kvm_write_guest_virt_helper()
7701 bytes -= towrite; in kvm_write_guest_virt_helper()
7729 vcpu->arch.l1tf_flush_l1d = true; in kvm_write_guest_virt_system()
7789 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vcpu_mmio_gva_to_gpa()
7799 !permission_fault(vcpu, vcpu->arch.walk_mmu, in vcpu_mmio_gva_to_gpa()
7800 vcpu->arch.mmio_access, 0, access))) { in vcpu_mmio_gva_to_gpa()
7801 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | in vcpu_mmio_gva_to_gpa()
7802 (gva & (PAGE_SIZE - 1)); in vcpu_mmio_gva_to_gpa()
7807 *gpa = mmu->gva_to_gpa(vcpu, mmu, gva, access, exception); in vcpu_mmio_gva_to_gpa()
7810 return -1; in vcpu_mmio_gva_to_gpa()
7841 if (vcpu->mmio_read_completed) { in read_prepare()
7843 vcpu->mmio_fragments[0].gpa, val); in read_prepare()
7844 vcpu->mmio_read_completed = 0; in read_prepare()
7879 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; in write_exit_mmio()
7881 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in write_exit_mmio()
7907 bool write = ops->write; in emulator_read_write_onepage()
7909 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in emulator_read_write_onepage()
7918 if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && in emulator_read_write_onepage()
7919 (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { in emulator_read_write_onepage()
7920 gpa = ctxt->gpa_val; in emulator_read_write_onepage()
7928 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) in emulator_read_write_onepage()
7934 handled = ops->read_write_mmio(vcpu, gpa, bytes, val); in emulator_read_write_onepage()
7939 bytes -= handled; in emulator_read_write_onepage()
7942 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); in emulator_read_write_onepage()
7943 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; in emulator_read_write_onepage()
7944 frag->gpa = gpa; in emulator_read_write_onepage()
7945 frag->data = val; in emulator_read_write_onepage()
7946 frag->len = bytes; in emulator_read_write_onepage()
7960 if (ops->read_write_prepare && in emulator_read_write()
7961 ops->read_write_prepare(vcpu, val, bytes)) in emulator_read_write()
7964 vcpu->mmio_nr_fragments = 0; in emulator_read_write()
7967 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { in emulator_read_write()
7970 now = -addr & ~PAGE_MASK; in emulator_read_write()
7977 if (ctxt->mode != X86EMUL_MODE_PROT64) in emulator_read_write()
7980 bytes -= now; in emulator_read_write()
7988 if (!vcpu->mmio_nr_fragments) in emulator_read_write()
7991 gpa = vcpu->mmio_fragments[0].gpa; in emulator_read_write()
7993 vcpu->mmio_needed = 1; in emulator_read_write()
7994 vcpu->mmio_cur_fragment = 0; in emulator_read_write()
7996 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); in emulator_read_write()
7997 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; in emulator_read_write()
7998 vcpu->run->exit_reason = KVM_EXIT_MMIO; in emulator_read_write()
7999 vcpu->run->mmio.phys_addr = gpa; in emulator_read_write()
8001 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); in emulator_read_write()
8041 if (bytes > 8 || (bytes & (bytes - 1))) in emulator_cmpxchg_emulated()
8055 page_line_mask = ~(cache_line_size() - 1); in emulator_cmpxchg_emulated()
8059 if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) in emulator_cmpxchg_emulated()
8117 WARN_ON_ONCE(vcpu->arch.pio.count); in emulator_pio_in_out()
8133 memset(data, 0, size * (count - i)); in emulator_pio_in_out()
8142 vcpu->arch.pio.port = port; in emulator_pio_in_out()
8143 vcpu->arch.pio.in = in; in emulator_pio_in_out()
8144 vcpu->arch.pio.count = count; in emulator_pio_in_out()
8145 vcpu->arch.pio.size = size; in emulator_pio_in_out()
8148 memset(vcpu->arch.pio_data, 0, size * count); in emulator_pio_in_out()
8150 memcpy(vcpu->arch.pio_data, data, size * count); in emulator_pio_in_out()
8152 vcpu->run->exit_reason = KVM_EXIT_IO; in emulator_pio_in_out()
8153 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; in emulator_pio_in_out()
8154 vcpu->run->io.size = size; in emulator_pio_in_out()
8155 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; in emulator_pio_in_out()
8156 vcpu->run->io.count = count; in emulator_pio_in_out()
8157 vcpu->run->io.port = port; in emulator_pio_in_out()
8173 int size = vcpu->arch.pio.size; in complete_emulator_pio_in()
8174 unsigned int count = vcpu->arch.pio.count; in complete_emulator_pio_in()
8175 memcpy(val, vcpu->arch.pio_data, size * count); in complete_emulator_pio_in()
8176 trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pio_data); in complete_emulator_pio_in()
8177 vcpu->arch.pio.count = 0; in complete_emulator_pio_in()
8185 if (vcpu->arch.pio.count) { in emulator_pio_in_emulated()
8215 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) in get_segment_base() argument
8217 return kvm_x86_call(get_segment_base)(vcpu, seg); in get_segment_base()
8233 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8234 on_each_cpu_mask(vcpu->arch.wbinvd_dirty_mask, in kvm_emulate_wbinvd_noskip()
8237 cpumask_clear(vcpu->arch.wbinvd_dirty_mask); in kvm_emulate_wbinvd_noskip()
8271 return (curr_cr & ~((1ULL << 32) - 1)) | new_val; in mk_cr_64()
8284 value = vcpu->arch.cr2; in emulator_get_cr()
8313 vcpu->arch.cr2 = val; in emulator_set_cr()
8326 res = -1; in emulator_set_cr()
8358 struct x86_emulate_ctxt *ctxt, int seg) in emulator_get_cached_segment_base() argument
8360 return get_segment_base(emul_to_vcpu(ctxt), seg); in emulator_get_cached_segment_base()
8365 int seg) in emulator_get_segment() argument
8369 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); in emulator_get_segment()
8387 desc->type = var.type; in emulator_get_segment()
8388 desc->s = var.s; in emulator_get_segment()
8389 desc->dpl = var.dpl; in emulator_get_segment()
8390 desc->p = var.present; in emulator_get_segment()
8391 desc->avl = var.avl; in emulator_get_segment()
8392 desc->l = var.l; in emulator_get_segment()
8393 desc->d = var.db; in emulator_get_segment()
8394 desc->g = var.g; in emulator_get_segment()
8401 int seg) in emulator_set_segment() argument
8412 if (desc->g) in emulator_set_segment()
8414 var.type = desc->type; in emulator_set_segment()
8415 var.dpl = desc->dpl; in emulator_set_segment()
8416 var.db = desc->d; in emulator_set_segment()
8417 var.s = desc->s; in emulator_set_segment()
8418 var.l = desc->l; in emulator_set_segment()
8419 var.g = desc->g; in emulator_set_segment()
8420 var.avl = desc->avl; in emulator_set_segment()
8421 var.present = desc->p; in emulator_set_segment()
8425 kvm_set_segment(vcpu, &var, seg); in emulator_set_segment()
8494 emul_to_vcpu(ctxt)->arch.halt_request = 1; in emulator_halt()
8502 &ctxt->exception); in emulator_intercept()
8577 struct kvm *kvm = emul_to_vcpu(ctxt)->kvm; in emulator_vm_bugged()
8579 if (!kvm->vm_bugged) in emulator_vm_bugged()
8669 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in inject_emulated_exception()
8671 if (ctxt->exception.vector == PF_VECTOR) in inject_emulated_exception()
8672 kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); in inject_emulated_exception()
8673 else if (ctxt->exception.error_code_valid) in inject_emulated_exception()
8674 kvm_queue_exception_e(vcpu, ctxt->exception.vector, in inject_emulated_exception()
8675 ctxt->exception.error_code); in inject_emulated_exception()
8677 kvm_queue_exception(vcpu, ctxt->exception.vector); in inject_emulated_exception()
8690 ctxt->vcpu = vcpu; in alloc_emulate_ctxt()
8691 ctxt->ops = &emulate_ops; in alloc_emulate_ctxt()
8692 vcpu->arch.emulate_ctxt = ctxt; in alloc_emulate_ctxt()
8699 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in init_emulate_ctxt()
8704 ctxt->gpa_available = false; in init_emulate_ctxt()
8705 ctxt->eflags = kvm_get_rflags(vcpu); in init_emulate_ctxt()
8706 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; in init_emulate_ctxt()
8708 ctxt->eip = kvm_rip_read(vcpu); in init_emulate_ctxt()
8709 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : in init_emulate_ctxt()
8710 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : in init_emulate_ctxt()
8714 ctxt->interruptibility = 0; in init_emulate_ctxt()
8715 ctxt->have_exception = false; in init_emulate_ctxt()
8716 ctxt->exception.vector = -1; in init_emulate_ctxt()
8717 ctxt->perm_ok = false; in init_emulate_ctxt()
8720 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in init_emulate_ctxt()
8725 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_inject_realmode_interrupt()
8730 ctxt->op_bytes = 2; in kvm_inject_realmode_interrupt()
8731 ctxt->ad_bytes = 2; in kvm_inject_realmode_interrupt()
8732 ctxt->_eip = ctxt->eip + inc_eip; in kvm_inject_realmode_interrupt()
8738 ctxt->eip = ctxt->_eip; in kvm_inject_realmode_interrupt()
8739 kvm_rip_write(vcpu, ctxt->eip); in kvm_inject_realmode_interrupt()
8740 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_inject_realmode_interrupt()
8748 struct kvm_run *run = vcpu->run; in prepare_emulation_failure_exit()
8761 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in prepare_emulation_failure_exit()
8762 run->emulation_failure.suberror = KVM_INTERNAL_ERROR_EMULATION; in prepare_emulation_failure_exit()
8774 run->emulation_failure.flags = 0; in prepare_emulation_failure_exit()
8777 BUILD_BUG_ON((sizeof(run->emulation_failure.insn_size) + in prepare_emulation_failure_exit()
8778 sizeof(run->emulation_failure.insn_bytes) != 16)); in prepare_emulation_failure_exit()
8780 run->emulation_failure.flags |= in prepare_emulation_failure_exit()
8782 run->emulation_failure.insn_size = insn_size; in prepare_emulation_failure_exit()
8783 memset(run->emulation_failure.insn_bytes, 0x90, in prepare_emulation_failure_exit()
8784 sizeof(run->emulation_failure.insn_bytes)); in prepare_emulation_failure_exit()
8785 memcpy(run->emulation_failure.insn_bytes, insn_bytes, insn_size); in prepare_emulation_failure_exit()
8788 memcpy(&run->internal.data[info_start], info, sizeof(info)); in prepare_emulation_failure_exit()
8789 memcpy(&run->internal.data[info_start + ARRAY_SIZE(info)], data, in prepare_emulation_failure_exit()
8792 run->emulation_failure.ndata = info_start + ARRAY_SIZE(info) + ndata; in prepare_emulation_failure_exit()
8797 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in prepare_emulation_ctxt_failure_exit()
8799 prepare_emulation_failure_exit(vcpu, NULL, 0, ctxt->fetch.data, in prepare_emulation_ctxt_failure_exit()
8800 ctxt->fetch.end - ctxt->fetch.data); in prepare_emulation_ctxt_failure_exit()
8818 struct kvm *kvm = vcpu->kvm; in handle_emulation_failure()
8820 ++vcpu->stat.insn_emulation_fail; in handle_emulation_failure()
8828 if (kvm->arch.exit_on_emulation_error || in handle_emulation_failure()
8857 * a SPTE and write-protect the gfn to resolve the !PRESENT fault, and in kvm_unprotect_and_retry_on_failure()
8866 * table, unprotect the gfn (zap any relevant SPTEs) and re-enter the in kvm_unprotect_and_retry_on_failure()
8867 * guest to let the CPU re-execute the instruction in the hope that the in kvm_unprotect_and_retry_on_failure()
8902 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_do_singlestep()
8904 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { in kvm_vcpu_do_singlestep()
8905 kvm_run->debug.arch.dr6 = DR6_BS | DR6_ACTIVE_LOW; in kvm_vcpu_do_singlestep()
8906 kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); in kvm_vcpu_do_singlestep()
8907 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_do_singlestep()
8908 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_do_singlestep()
8978 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && in kvm_vcpu_check_code_breakpoint()
8979 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { in kvm_vcpu_check_code_breakpoint()
8980 struct kvm_run *kvm_run = vcpu->run; in kvm_vcpu_check_code_breakpoint()
8983 vcpu->arch.guest_debug_dr7, in kvm_vcpu_check_code_breakpoint()
8984 vcpu->arch.eff_db); in kvm_vcpu_check_code_breakpoint()
8987 kvm_run->debug.arch.dr6 = dr6 | DR6_ACTIVE_LOW; in kvm_vcpu_check_code_breakpoint()
8988 kvm_run->debug.arch.pc = eip; in kvm_vcpu_check_code_breakpoint()
8989 kvm_run->debug.arch.exception = DB_VECTOR; in kvm_vcpu_check_code_breakpoint()
8990 kvm_run->exit_reason = KVM_EXIT_DEBUG; in kvm_vcpu_check_code_breakpoint()
8996 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && in kvm_vcpu_check_code_breakpoint()
9000 vcpu->arch.dr7, in kvm_vcpu_check_code_breakpoint()
9001 vcpu->arch.db); in kvm_vcpu_check_code_breakpoint()
9015 switch (ctxt->opcode_len) { in is_vmware_backdoor_opcode()
9017 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9034 switch (ctxt->b) { in is_vmware_backdoor_opcode()
9047 * (and wrong) when emulating on an intercepted fault-like exception[*], as
9057 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_decode_emulated_instruction()
9065 ++vcpu->stat.insn_emulation; in x86_decode_emulated_instruction()
9075 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in x86_emulate_instruction()
9092 vcpu->arch.l1tf_flush_l1d = true; in x86_emulate_instruction()
9099 * are fault-like and are higher priority than any faults on in x86_emulate_instruction()
9117 if (ctxt->have_exception && in x86_emulate_instruction()
9120 * #UD should result in just EMULATION_FAILED, and trap-like in x86_emulate_instruction()
9123 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || in x86_emulate_instruction()
9124 exception_type(ctxt->exception.vector) == EXCPT_TRAP); in x86_emulate_instruction()
9142 * injecting single-step #DBs. in x86_emulate_instruction()
9145 if (ctxt->mode != X86EMUL_MODE_PROT64) in x86_emulate_instruction()
9146 ctxt->eip = (u32)ctxt->_eip; in x86_emulate_instruction()
9148 ctxt->eip = ctxt->_eip; in x86_emulate_instruction()
9155 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9156 if (ctxt->eflags & X86_EFLAGS_RF) in x86_emulate_instruction()
9157 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); in x86_emulate_instruction()
9162 * If emulation was caused by a write-protection #PF on a non-page_table in x86_emulate_instruction()
9174 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { in x86_emulate_instruction()
9175 vcpu->arch.emulate_regs_need_sync_from_vcpu = false; in x86_emulate_instruction()
9182 ctxt->exception.address = cr2_or_gpa; in x86_emulate_instruction()
9185 if (vcpu->arch.mmu->root_role.direct) { in x86_emulate_instruction()
9186 ctxt->gpa_available = true; in x86_emulate_instruction()
9187 ctxt->gpa_val = cr2_or_gpa; in x86_emulate_instruction()
9191 ctxt->exception.address = 0; in x86_emulate_instruction()
9207 if (ctxt->have_exception) { in x86_emulate_instruction()
9208 WARN_ON_ONCE(vcpu->mmio_needed && !vcpu->mmio_is_write); in x86_emulate_instruction()
9209 vcpu->mmio_needed = false; in x86_emulate_instruction()
9212 } else if (vcpu->arch.pio.count) { in x86_emulate_instruction()
9213 if (!vcpu->arch.pio.in) { in x86_emulate_instruction()
9214 /* FIXME: return into emulator if single-stepping. */ in x86_emulate_instruction()
9215 vcpu->arch.pio.count = 0; in x86_emulate_instruction()
9218 vcpu->arch.complete_userspace_io = complete_emulated_pio; in x86_emulate_instruction()
9221 } else if (vcpu->mmio_needed) { in x86_emulate_instruction()
9222 ++vcpu->stat.mmio_exits; in x86_emulate_instruction()
9224 if (!vcpu->mmio_is_write) in x86_emulate_instruction()
9227 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in x86_emulate_instruction()
9228 } else if (vcpu->arch.complete_userspace_io) { in x86_emulate_instruction()
9239 toggle_interruptibility(vcpu, ctxt->interruptibility); in x86_emulate_instruction()
9240 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in x86_emulate_instruction()
9243 * Note, EXCPT_DB is assumed to be fault-like as the emulator in x86_emulate_instruction()
9245 * of which are fault-like. in x86_emulate_instruction()
9247 if (!ctxt->have_exception || in x86_emulate_instruction()
9248 exception_type(ctxt->exception.vector) == EXCPT_TRAP) { in x86_emulate_instruction()
9250 if (ctxt->is_branch) in x86_emulate_instruction()
9252 kvm_rip_write(vcpu, ctxt->eip); in x86_emulate_instruction()
9253 if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) in x86_emulate_instruction()
9256 __kvm_set_rflags(vcpu, ctxt->eflags); in x86_emulate_instruction()
9265 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) in x86_emulate_instruction()
9268 vcpu->arch.emulate_regs_need_sync_to_vcpu = true; in x86_emulate_instruction()
9288 vcpu->arch.pio.count = 0; in complete_fast_pio_out_port_0x7e()
9294 vcpu->arch.pio.count = 0; in complete_fast_pio_out()
9296 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) in complete_fast_pio_out()
9316 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { in kvm_fast_pio_out()
9317 vcpu->arch.complete_userspace_io = in kvm_fast_pio_out()
9321 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_out()
9322 vcpu->arch.complete_userspace_io = complete_fast_pio_out; in kvm_fast_pio_out()
9332 BUG_ON(vcpu->arch.pio.count != 1); in complete_fast_pio_in()
9334 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { in complete_fast_pio_in()
9335 vcpu->arch.pio.count = 0; in complete_fast_pio_in()
9340 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; in complete_fast_pio_in()
9363 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); in kvm_fast_pio_in()
9364 vcpu->arch.complete_userspace_io = complete_fast_pio_in; in kvm_fast_pio_in()
9395 khz = freq->new; in tsc_khz_changed()
9416 /* TSC frequency always matches when on Hyper-V */ in kvm_hyperv_tsc_notifier()
9484 if (vcpu->cpu != cpu) in __kvmclock_cpufreq_notifier()
9487 if (vcpu->cpu != raw_smp_processor_id()) in __kvmclock_cpufreq_notifier()
9493 if (freq->old < freq->new && send_ipi) { in __kvmclock_cpufreq_notifier()
9516 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) in kvmclock_cpufreq_notifier()
9518 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) in kvmclock_cpufreq_notifier()
9521 for_each_cpu(cpu, freq->policy->cpus) in kvmclock_cpufreq_notifier()
9549 if (policy->cpuinfo.max_freq) in kvm_timer_init()
9550 max_tsc_khz = policy->cpuinfo.max_freq; in kvm_timer_init()
9608 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && in pvclock_gtod_notify()
9621 memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); in kvm_ops_update()
9631 #include <asm/kvm-x86-ops.h> in kvm_ops_update()
9634 kvm_pmu_ops_update(ops->pmu_ops); in kvm_ops_update()
9652 return -EIO; in kvm_x86_check_processor_compatibility()
9671 return -EEXIST; in kvm_x86_vendor_init()
9681 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9686 return -EOPNOTSUPP; in kvm_x86_vendor_init()
9699 return -EIO; in kvm_x86_vendor_init()
9707 return -ENOMEM; in kvm_x86_vendor_init()
9713 r = -ENOMEM; in kvm_x86_vendor_init()
9735 kvm_init_pmu_capability(ops->pmu_ops); in kvm_x86_vendor_init()
9740 r = ops->hardware_setup(); in kvm_x86_vendor_init()
9759 if (pi_inject_timer == -1) in kvm_x86_vendor_init()
9768 kvm_register_perf_callbacks(ops->handle_intel_pt_intr); in kvm_x86_vendor_init()
9852 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9858 if (vcpu->arch.tsc_always_catchup) in kvm_pv_clock_pairing()
9859 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9862 return -KVM_EOPNOTSUPP; in kvm_pv_clock_pairing()
9871 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, in kvm_pv_clock_pairing()
9873 ret = -KVM_EFAULT; in kvm_pv_clock_pairing()
9882 * @apicid - apicid of vcpu to be kicked.
9902 return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); in kvm_apicv_activated()
9908 ulong vm_reasons = READ_ONCE(vcpu->kvm->arch.apicv_inhibit_reasons); in kvm_vcpu_apicv_activated()
9936 set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason, true); in kvm_apicv_init()
9938 init_rwsem(&kvm->arch.apicv_update_lock); in kvm_apicv_init()
9946 vcpu->stat.directed_yield_attempted++; in kvm_sched_yield()
9952 map = rcu_dereference(vcpu->kvm->arch.apic_map); in kvm_sched_yield()
9954 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) in kvm_sched_yield()
9955 target = map->phys_map[dest_id]->vcpu; in kvm_sched_yield()
9959 if (!target || !READ_ONCE(target->ready)) in kvm_sched_yield()
9969 vcpu->stat.directed_yield_successful++; in kvm_sched_yield()
9977 u64 ret = vcpu->run->hypercall.ret; in complete_hypercall_exit()
9982 ++vcpu->stat.hypercalls; in complete_hypercall_exit()
10004 ret = -KVM_EPERM; in __kvm_emulate_hypercall()
10008 ret = -KVM_ENOSYS; in __kvm_emulate_hypercall()
10018 kvm_pv_kick_cpu_op(vcpu->kvm, a1); in __kvm_emulate_hypercall()
10031 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); in __kvm_emulate_hypercall()
10043 ret = -KVM_ENOSYS; in __kvm_emulate_hypercall()
10044 if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) in __kvm_emulate_hypercall()
10049 ret = -KVM_EINVAL; in __kvm_emulate_hypercall()
10053 vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; in __kvm_emulate_hypercall()
10054 vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; in __kvm_emulate_hypercall()
10055 vcpu->run->hypercall.args[0] = gpa; in __kvm_emulate_hypercall()
10056 vcpu->run->hypercall.args[1] = npages; in __kvm_emulate_hypercall()
10057 vcpu->run->hypercall.args[2] = attrs; in __kvm_emulate_hypercall()
10058 vcpu->run->hypercall.flags = 0; in __kvm_emulate_hypercall()
10060 vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE; in __kvm_emulate_hypercall()
10062 WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); in __kvm_emulate_hypercall()
10063 vcpu->arch.complete_userspace_io = complete_hypercall_exit; in __kvm_emulate_hypercall()
10068 ret = -KVM_ENOSYS; in __kvm_emulate_hypercall()
10073 ++vcpu->stat.hypercalls; in __kvm_emulate_hypercall()
10084 if (kvm_xen_hypercall_enabled(vcpu->kvm)) in kvm_emulate_hypercall()
10121 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_FIX_HYPERCALL_INSN)) { in emulator_fix_hypercall()
10122 ctxt->exception.error_code_valid = false; in emulator_fix_hypercall()
10123 ctxt->exception.vector = UD_VECTOR; in emulator_fix_hypercall()
10124 ctxt->have_exception = true; in emulator_fix_hypercall()
10131 &ctxt->exception); in emulator_fix_hypercall()
10136 return vcpu->run->request_interrupt_window && in dm_request_for_irq_injection()
10137 likely(!pic_in_kernel(vcpu->kvm)); in dm_request_for_irq_injection()
10140 /* Called within kvm->srcu read side. */
10143 struct kvm_run *kvm_run = vcpu->run; in post_kvm_run_save()
10145 kvm_run->if_flag = kvm_x86_call(get_if_flag)(vcpu); in post_kvm_run_save()
10146 kvm_run->cr8 = kvm_get_cr8(vcpu); in post_kvm_run_save()
10147 kvm_run->apic_base = vcpu->arch.apic_base; in post_kvm_run_save()
10149 kvm_run->ready_for_interrupt_injection = in post_kvm_run_save()
10150 pic_in_kernel(vcpu->kvm) || in post_kvm_run_save()
10154 kvm_run->flags |= KVM_RUN_X86_SMM; in post_kvm_run_save()
10156 kvm_run->flags |= KVM_RUN_X86_GUEST_MODE; in post_kvm_run_save()
10169 if (vcpu->arch.apic->apicv_active) in update_cr8_intercept()
10172 if (!vcpu->arch.apic->vapic_addr) in update_cr8_intercept()
10175 max_irr = -1; in update_cr8_intercept()
10177 if (max_irr != -1) in update_cr8_intercept()
10189 kvm_x86_ops.nested_ops->triple_fault(vcpu); in kvm_check_nested_events()
10193 return kvm_x86_ops.nested_ops->check_events(vcpu); in kvm_check_nested_events()
10202 * is injected as intercepted #PF VM-Exits for AMD's Paged Real Mode do in kvm_inject_exception()
10205 vcpu->arch.exception.has_error_code &= is_protmode(vcpu); in kvm_inject_exception()
10207 trace_kvm_inj_exception(vcpu->arch.exception.vector, in kvm_inject_exception()
10208 vcpu->arch.exception.has_error_code, in kvm_inject_exception()
10209 vcpu->arch.exception.error_code, in kvm_inject_exception()
10210 vcpu->arch.exception.injected); in kvm_inject_exception()
10220 * injected as part of a previous VM-Enter, but weren't successfully delivered
10221 * and need to be re-injected.
10226 * also be able to re-inject NMIs and IRQs in the middle of an instruction.
10227 * I.e. for exceptions and re-injected events, NOT invoking this on instruction
10232 * instruction boundaries for asynchronous events. However, because VM-Exits
10238 * But, if a VM-Exit occurs during instruction execution, and KVM does NOT skip
10261 * Process nested events first, as nested VM-Exit supersedes event in kvm_check_and_inject_events()
10262 * re-injection. If there's an event queued for re-injection, it will in kvm_check_and_inject_events()
10263 * be saved into the appropriate vmc{b,s}12 fields on nested VM-Exit. in kvm_check_and_inject_events()
10271 * Re-inject exceptions and events *especially* if immediate entry+exit in kvm_check_and_inject_events()
10275 * Don't re-inject an NMI or interrupt if there is a pending exception. in kvm_check_and_inject_events()
10284 * as the exception "occurred" before the exit to userspace. Trap-like in kvm_check_and_inject_events()
10286 * And while fault-like exceptions, e.g. #GP and #PF, are the lowest in kvm_check_and_inject_events()
10289 * Thus a pending fault-like exception means the fault occurred on the in kvm_check_and_inject_events()
10293 if (vcpu->arch.exception.injected) in kvm_check_and_inject_events()
10297 else if (vcpu->arch.nmi_injected) in kvm_check_and_inject_events()
10299 else if (vcpu->arch.interrupt.injected) in kvm_check_and_inject_events()
10303 * Exceptions that morph to VM-Exits are handled above, and pending in kvm_check_and_inject_events()
10304 * exceptions on top of injected exceptions that do not VM-Exit should in kvm_check_and_inject_events()
10307 WARN_ON_ONCE(vcpu->arch.exception.injected && in kvm_check_and_inject_events()
10308 vcpu->arch.exception.pending); in kvm_check_and_inject_events()
10312 * nested VM-Enter or event re-injection so that a different pending in kvm_check_and_inject_events()
10315 * Otherwise, continue processing events even if VM-Exit occurred. The in kvm_check_and_inject_events()
10316 * VM-Exit will have cleared exceptions that were meant for L2, but in kvm_check_and_inject_events()
10323 * A pending exception VM-Exit should either result in nested VM-Exit in kvm_check_and_inject_events()
10324 * or force an immediate re-entry and exit to/from L2, and exception in kvm_check_and_inject_events()
10325 * VM-Exits cannot be injected (flag should _never_ be set). in kvm_check_and_inject_events()
10327 WARN_ON_ONCE(vcpu->arch.exception_vmexit.injected || in kvm_check_and_inject_events()
10328 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10332 * to re-inject a previous event. See above comments on re-injecting in kvm_check_and_inject_events()
10337 if (vcpu->arch.exception.pending) { in kvm_check_and_inject_events()
10339 * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS in kvm_check_and_inject_events()
10340 * value pushed on the stack. Trap-like exception and all #DBs in kvm_check_and_inject_events()
10341 * leave RF as-is (KVM follows Intel's behavior in this regard; in kvm_check_and_inject_events()
10346 * fault-like. They do _not_ set RF, a la code breakpoints. in kvm_check_and_inject_events()
10348 if (exception_type(vcpu->arch.exception.vector) == EXCPT_FAULT) in kvm_check_and_inject_events()
10352 if (vcpu->arch.exception.vector == DB_VECTOR) { in kvm_check_and_inject_events()
10353 kvm_deliver_exception_payload(vcpu, &vcpu->arch.exception); in kvm_check_and_inject_events()
10354 if (vcpu->arch.dr7 & DR7_GD) { in kvm_check_and_inject_events()
10355 vcpu->arch.dr7 &= ~DR7_GD; in kvm_check_and_inject_events()
10362 vcpu->arch.exception.pending = false; in kvm_check_and_inject_events()
10363 vcpu->arch.exception.injected = true; in kvm_check_and_inject_events()
10369 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) in kvm_check_and_inject_events()
10374 * due to architectural conditions (e.g. IF=0) a window-open exit in kvm_check_and_inject_events()
10375 * will re-request KVM_REQ_EVENT. Sometimes however an event is pending in kvm_check_and_inject_events()
10381 * The kvm_x86_ops hooks communicate this by returning -EBUSY. in kvm_check_and_inject_events()
10384 if (vcpu->arch.smi_pending) { in kvm_check_and_inject_events()
10386 -EBUSY; in kvm_check_and_inject_events()
10390 vcpu->arch.smi_pending = false; in kvm_check_and_inject_events()
10391 ++vcpu->arch.smi_count; in kvm_check_and_inject_events()
10399 if (vcpu->arch.nmi_pending) { in kvm_check_and_inject_events()
10401 -EBUSY; in kvm_check_and_inject_events()
10405 --vcpu->arch.nmi_pending; in kvm_check_and_inject_events()
10406 vcpu->arch.nmi_injected = true; in kvm_check_and_inject_events()
10411 if (vcpu->arch.nmi_pending) in kvm_check_and_inject_events()
10417 -EBUSY; in kvm_check_and_inject_events()
10423 if (!WARN_ON_ONCE(irq == -1)) { in kvm_check_and_inject_events()
10434 kvm_x86_ops.nested_ops->has_events && in kvm_check_and_inject_events()
10435 kvm_x86_ops.nested_ops->has_events(vcpu, true)) in kvm_check_and_inject_events()
10440 * is done emulating and should only propagate the to-be-injected event in kvm_check_and_inject_events()
10442 * infinite loop as KVM will bail from VM-Enter to inject the pending in kvm_check_and_inject_events()
10450 WARN_ON_ONCE(vcpu->arch.exception.pending || in kvm_check_and_inject_events()
10451 vcpu->arch.exception_vmexit.pending); in kvm_check_and_inject_events()
10455 if (r == -EBUSY) { in kvm_check_and_inject_events()
10476 if (kvm_x86_call(get_nmi_mask)(vcpu) || vcpu->arch.nmi_injected) in process_nmi()
10483 * tracked in vcpu->arch.nmi_pending. in process_nmi()
10486 limit--; in process_nmi()
10488 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); in process_nmi()
10489 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); in process_nmi()
10491 if (vcpu->arch.nmi_pending && in process_nmi()
10493 vcpu->arch.nmi_pending--; in process_nmi()
10495 if (vcpu->arch.nmi_pending) in process_nmi()
10502 return vcpu->arch.nmi_pending + in kvm_get_nr_pending_nmis()
10519 struct kvm_lapic *apic = vcpu->arch.apic; in __kvm_vcpu_update_apicv()
10525 down_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10532 if (apic->apicv_active == activate) in __kvm_vcpu_update_apicv()
10535 apic->apicv_active = activate; in __kvm_vcpu_update_apicv()
10545 if (!apic->apicv_active) in __kvm_vcpu_update_apicv()
10550 up_read(&vcpu->kvm->arch.apicv_update_lock); in __kvm_vcpu_update_apicv()
10570 if (apic_x2apic_mode(vcpu->arch.apic) && in kvm_vcpu_update_apicv()
10582 lockdep_assert_held_write(&kvm->arch.apicv_update_lock); in __kvm_set_or_clear_apicv_inhibit()
10587 old = new = kvm->arch.apicv_inhibit_reasons; in __kvm_set_or_clear_apicv_inhibit()
10605 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10608 int idx = srcu_read_lock(&kvm->srcu); in __kvm_set_or_clear_apicv_inhibit()
10611 srcu_read_unlock(&kvm->srcu, idx); in __kvm_set_or_clear_apicv_inhibit()
10614 kvm->arch.apicv_inhibit_reasons = new; in __kvm_set_or_clear_apicv_inhibit()
10624 down_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10626 up_write(&kvm->arch.apicv_update_lock); in kvm_set_or_clear_apicv_inhibit()
10635 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); in vcpu_scan_ioapic()
10639 if (irqchip_split(vcpu->kvm)) in vcpu_scan_ioapic()
10640 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10641 else if (ioapic_in_kernel(vcpu->kvm)) in vcpu_scan_ioapic()
10642 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); in vcpu_scan_ioapic()
10645 vcpu->arch.load_eoi_exitmap_pending = true; in vcpu_scan_ioapic()
10652 if (!kvm_apic_hw_enabled(vcpu->arch.apic)) in vcpu_load_eoi_exitmap()
10660 vcpu->arch.ioapic_handled_vectors, in vcpu_load_eoi_exitmap()
10661 to_hv_synic(vcpu)->vec_bitmap, 256); in vcpu_load_eoi_exitmap()
10667 vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors); in vcpu_load_eoi_exitmap()
10684 * Called within kvm->srcu read side.
10701 r = -EIO; in vcpu_enter_guest()
10711 if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { in vcpu_enter_guest()
10721 kvm_update_masterclock(vcpu->kvm); in vcpu_enter_guest()
10745 * Fall back to a "full" guest flush if Hyper-V's precise in vcpu_enter_guest()
10746 * flushing fails. Note, Hyper-V's flushing is per-vCPU, but in vcpu_enter_guest()
10757 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; in vcpu_enter_guest()
10763 kvm_x86_ops.nested_ops->triple_fault(vcpu); in vcpu_enter_guest()
10766 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in vcpu_enter_guest()
10767 vcpu->mmio_needed = 0; in vcpu_enter_guest()
10774 vcpu->arch.apf.halted = true; in vcpu_enter_guest()
10791 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); in vcpu_enter_guest()
10792 if (test_bit(vcpu->arch.pending_ioapic_eoi, in vcpu_enter_guest()
10793 vcpu->arch.ioapic_handled_vectors)) { in vcpu_enter_guest()
10794 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; in vcpu_enter_guest()
10795 vcpu->run->eoi.vector = in vcpu_enter_guest()
10796 vcpu->arch.pending_ioapic_eoi; in vcpu_enter_guest()
10809 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10810 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; in vcpu_enter_guest()
10811 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10816 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; in vcpu_enter_guest()
10817 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; in vcpu_enter_guest()
10818 vcpu->run->system_event.ndata = 0; in vcpu_enter_guest()
10825 vcpu->run->exit_reason = KVM_EXIT_HYPERV; in vcpu_enter_guest()
10826 vcpu->run->hyperv = hv_vcpu->exit; in vcpu_enter_guest()
10833 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers in vcpu_enter_guest()
10834 * depend on the guest clock being up-to-date in vcpu_enter_guest()
10851 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { in vcpu_enter_guest()
10860 ++vcpu->stat.req_event; in vcpu_enter_guest()
10866 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { in vcpu_enter_guest()
10901 /* Store vcpu->apicv_active before vcpu->mode. */ in vcpu_enter_guest()
10902 smp_store_release(&vcpu->mode, IN_GUEST_MODE); in vcpu_enter_guest()
10907 * 1) We should set ->mode before checking ->requests. Please see in vcpu_enter_guest()
10910 * 2) For APICv, we should set ->mode before checking PID.ON. This in vcpu_enter_guest()
10931 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
10947 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
10948 wrmsrl(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err); in vcpu_enter_guest()
10950 if (unlikely(vcpu->arch.switch_db_regs)) { in vcpu_enter_guest()
10951 set_debugreg(0, 7); in vcpu_enter_guest()
10952 set_debugreg(vcpu->arch.eff_db[0], 0); in vcpu_enter_guest()
10953 set_debugreg(vcpu->arch.eff_db[1], 1); in vcpu_enter_guest()
10954 set_debugreg(vcpu->arch.eff_db[2], 2); in vcpu_enter_guest()
10955 set_debugreg(vcpu->arch.eff_db[3], 3); in vcpu_enter_guest()
10957 set_debugreg(0, 7); in vcpu_enter_guest()
10966 * per-VM state, and responding vCPUs must wait for the update in vcpu_enter_guest()
10985 /* Note, VM-Exits that go down the "slow" path are accounted below. */ in vcpu_enter_guest()
10986 ++vcpu->stat.exits; in vcpu_enter_guest()
10995 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { in vcpu_enter_guest()
10996 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); in vcpu_enter_guest()
11012 vcpu->arch.last_vmentry_cpu = vcpu->cpu; in vcpu_enter_guest()
11013 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); in vcpu_enter_guest()
11015 vcpu->mode = OUTSIDE_GUEST_MODE; in vcpu_enter_guest()
11020 * rely on the fact that guest_fpu::xfd is up-to-date (e.g. in vcpu_enter_guest()
11023 if (vcpu->arch.xfd_no_write_intercept) in vcpu_enter_guest()
11028 if (vcpu->arch.guest_fpu.xfd_err) in vcpu_enter_guest()
11033 * VM-Exit on SVM and any ticks that occur between VM-Exit and now. in vcpu_enter_guest()
11040 ++vcpu->stat.exits; in vcpu_enter_guest()
11072 if (unlikely(vcpu->arch.tsc_always_catchup)) in vcpu_enter_guest()
11075 if (vcpu->arch.apic_attention) in vcpu_enter_guest()
11088 if (unlikely(vcpu->arch.apic_attention)) in vcpu_enter_guest()
11096 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && in kvm_vcpu_running()
11097 !vcpu->arch.apf.halted); in kvm_vcpu_running()
11102 if (!list_empty_careful(&vcpu->async_pf.done)) in kvm_vcpu_has_events()
11109 if (vcpu->arch.pv.pv_unhalted) in kvm_vcpu_has_events()
11116 (vcpu->arch.nmi_pending && in kvm_vcpu_has_events()
11122 (vcpu->arch.smi_pending && in kvm_vcpu_has_events()
11140 kvm_x86_ops.nested_ops->has_events && in kvm_vcpu_has_events()
11141 kvm_x86_ops.nested_ops->has_events(vcpu, false)) in kvm_vcpu_has_events()
11155 /* Called within kvm->srcu read side. */
11162 * Switch to the software timer before halt-polling/blocking as in vcpu_block()
11165 * Switch before halt-polling so that KVM recognizes an expired in vcpu_block()
11173 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) in vcpu_block()
11194 * state field (AMD does not have a similar field and a VM-Exit always in vcpu_block()
11200 WARN_ON_ONCE(r == -EBUSY); in vcpu_block()
11207 switch(vcpu->arch.mp_state) { in vcpu_block()
11210 vcpu->arch.pv.pv_unhalted = false; in vcpu_block()
11211 vcpu->arch.mp_state = in vcpu_block()
11215 vcpu->arch.apf.halted = false; in vcpu_block()
11226 /* Called within kvm->srcu read side. */
11231 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; in vcpu_run()
11240 vcpu->arch.at_instruction_boundary = false; in vcpu_run()
11260 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; in vcpu_run()
11261 ++vcpu->stat.request_irq_exits; in vcpu_run()
11281 * local APIC is in-kernel, the run loop will detect the non-runnable in __kvm_emulate_halt()
11286 ++vcpu->stat.halt_exits; in __kvm_emulate_halt()
11289 vcpu->arch.pv.pv_unhalted = false; in __kvm_emulate_halt()
11291 vcpu->arch.mp_state = state; in __kvm_emulate_halt()
11294 vcpu->run->exit_reason = reason; in __kvm_emulate_halt()
11309 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered in kvm_emulate_halt()
11351 return vcpu->arch.preempted_in_kernel; in kvm_arch_vcpu_preempted_in_kernel()
11356 if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) in kvm_arch_dy_runnable()
11376 BUG_ON(!vcpu->arch.pio.count); in complete_emulated_pio()
11401 struct kvm_run *run = vcpu->run; in complete_emulated_mmio()
11405 BUG_ON(!vcpu->mmio_needed); in complete_emulated_mmio()
11408 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_emulated_mmio()
11409 len = min(8u, frag->len); in complete_emulated_mmio()
11410 if (!vcpu->mmio_is_write) in complete_emulated_mmio()
11411 memcpy(frag->data, run->mmio.data, len); in complete_emulated_mmio()
11413 if (frag->len <= 8) { in complete_emulated_mmio()
11416 vcpu->mmio_cur_fragment++; in complete_emulated_mmio()
11419 frag->data += len; in complete_emulated_mmio()
11420 frag->gpa += len; in complete_emulated_mmio()
11421 frag->len -= len; in complete_emulated_mmio()
11424 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_emulated_mmio()
11425 vcpu->mmio_needed = 0; in complete_emulated_mmio()
11427 /* FIXME: return into emulator if single-stepping. */ in complete_emulated_mmio()
11428 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11430 vcpu->mmio_read_completed = 1; in complete_emulated_mmio()
11434 run->exit_reason = KVM_EXIT_MMIO; in complete_emulated_mmio()
11435 run->mmio.phys_addr = frag->gpa; in complete_emulated_mmio()
11436 if (vcpu->mmio_is_write) in complete_emulated_mmio()
11437 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_emulated_mmio()
11438 run->mmio.len = min(8u, frag->len); in complete_emulated_mmio()
11439 run->mmio.is_write = vcpu->mmio_is_write; in complete_emulated_mmio()
11440 vcpu->arch.complete_userspace_io = complete_emulated_mmio; in complete_emulated_mmio()
11447 /* Exclude PKRU, it's restored separately immediately after VM-Exit. */ in kvm_load_guest_fpu()
11448 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, true); in kvm_load_guest_fpu()
11455 fpu_swap_kvm_fpstate(&vcpu->arch.guest_fpu, false); in kvm_put_guest_fpu()
11456 ++vcpu->stat.fpu_reload; in kvm_put_guest_fpu()
11462 struct kvm_queued_exception *ex = &vcpu->arch.exception; in kvm_arch_vcpu_ioctl_run()
11463 struct kvm_run *kvm_run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
11468 kvm_run->flags = 0; in kvm_arch_vcpu_ioctl_run()
11472 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { in kvm_arch_vcpu_ioctl_run()
11473 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11474 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11496 r = -EAGAIN; in kvm_arch_vcpu_ioctl_run()
11498 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11499 kvm_run->exit_reason = KVM_EXIT_INTR; in kvm_arch_vcpu_ioctl_run()
11500 ++vcpu->stat.signal_exits; in kvm_arch_vcpu_ioctl_run()
11505 if ((kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) || in kvm_arch_vcpu_ioctl_run()
11506 (kvm_run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)) { in kvm_arch_vcpu_ioctl_run()
11507 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11511 if (kvm_run->kvm_dirty_regs) { in kvm_arch_vcpu_ioctl_run()
11517 /* re-sync apic's tpr */ in kvm_arch_vcpu_ioctl_run()
11519 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { in kvm_arch_vcpu_ioctl_run()
11520 r = -EINVAL; in kvm_arch_vcpu_ioctl_run()
11527 * a pending VM-Exit if L1 wants to intercept the exception. in kvm_arch_vcpu_ioctl_run()
11529 if (vcpu->arch.exception_from_userspace && is_guest_mode(vcpu) && in kvm_arch_vcpu_ioctl_run()
11530 kvm_x86_ops.nested_ops->is_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11531 ex->error_code)) { in kvm_arch_vcpu_ioctl_run()
11532 kvm_queue_exception_vmexit(vcpu, ex->vector, in kvm_arch_vcpu_ioctl_run()
11533 ex->has_error_code, ex->error_code, in kvm_arch_vcpu_ioctl_run()
11534 ex->has_payload, ex->payload); in kvm_arch_vcpu_ioctl_run()
11535 ex->injected = false; in kvm_arch_vcpu_ioctl_run()
11536 ex->pending = false; in kvm_arch_vcpu_ioctl_run()
11538 vcpu->arch.exception_from_userspace = false; in kvm_arch_vcpu_ioctl_run()
11540 if (unlikely(vcpu->arch.complete_userspace_io)) { in kvm_arch_vcpu_ioctl_run()
11541 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; in kvm_arch_vcpu_ioctl_run()
11542 vcpu->arch.complete_userspace_io = NULL; in kvm_arch_vcpu_ioctl_run()
11547 WARN_ON_ONCE(vcpu->arch.pio.count); in kvm_arch_vcpu_ioctl_run()
11548 WARN_ON_ONCE(vcpu->mmio_needed); in kvm_arch_vcpu_ioctl_run()
11551 if (!vcpu->wants_to_run) { in kvm_arch_vcpu_ioctl_run()
11552 r = -EINTR; in kvm_arch_vcpu_ioctl_run()
11564 if (kvm_run->kvm_valid_regs) in kvm_arch_vcpu_ioctl_run()
11576 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { in __get_regs()
11584 emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); in __get_regs()
11585 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __get_regs()
11587 regs->rax = kvm_rax_read(vcpu); in __get_regs()
11588 regs->rbx = kvm_rbx_read(vcpu); in __get_regs()
11589 regs->rcx = kvm_rcx_read(vcpu); in __get_regs()
11590 regs->rdx = kvm_rdx_read(vcpu); in __get_regs()
11591 regs->rsi = kvm_rsi_read(vcpu); in __get_regs()
11592 regs->rdi = kvm_rdi_read(vcpu); in __get_regs()
11593 regs->rsp = kvm_rsp_read(vcpu); in __get_regs()
11594 regs->rbp = kvm_rbp_read(vcpu); in __get_regs()
11596 regs->r8 = kvm_r8_read(vcpu); in __get_regs()
11597 regs->r9 = kvm_r9_read(vcpu); in __get_regs()
11598 regs->r10 = kvm_r10_read(vcpu); in __get_regs()
11599 regs->r11 = kvm_r11_read(vcpu); in __get_regs()
11600 regs->r12 = kvm_r12_read(vcpu); in __get_regs()
11601 regs->r13 = kvm_r13_read(vcpu); in __get_regs()
11602 regs->r14 = kvm_r14_read(vcpu); in __get_regs()
11603 regs->r15 = kvm_r15_read(vcpu); in __get_regs()
11606 regs->rip = kvm_rip_read(vcpu); in __get_regs()
11607 regs->rflags = kvm_get_rflags(vcpu); in __get_regs()
11612 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_regs()
11613 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_regs()
11614 return -EINVAL; in kvm_arch_vcpu_ioctl_get_regs()
11624 vcpu->arch.emulate_regs_need_sync_from_vcpu = true; in __set_regs()
11625 vcpu->arch.emulate_regs_need_sync_to_vcpu = false; in __set_regs()
11627 kvm_rax_write(vcpu, regs->rax); in __set_regs()
11628 kvm_rbx_write(vcpu, regs->rbx); in __set_regs()
11629 kvm_rcx_write(vcpu, regs->rcx); in __set_regs()
11630 kvm_rdx_write(vcpu, regs->rdx); in __set_regs()
11631 kvm_rsi_write(vcpu, regs->rsi); in __set_regs()
11632 kvm_rdi_write(vcpu, regs->rdi); in __set_regs()
11633 kvm_rsp_write(vcpu, regs->rsp); in __set_regs()
11634 kvm_rbp_write(vcpu, regs->rbp); in __set_regs()
11636 kvm_r8_write(vcpu, regs->r8); in __set_regs()
11637 kvm_r9_write(vcpu, regs->r9); in __set_regs()
11638 kvm_r10_write(vcpu, regs->r10); in __set_regs()
11639 kvm_r11_write(vcpu, regs->r11); in __set_regs()
11640 kvm_r12_write(vcpu, regs->r12); in __set_regs()
11641 kvm_r13_write(vcpu, regs->r13); in __set_regs()
11642 kvm_r14_write(vcpu, regs->r14); in __set_regs()
11643 kvm_r15_write(vcpu, regs->r15); in __set_regs()
11646 kvm_rip_write(vcpu, regs->rip); in __set_regs()
11647 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); in __set_regs()
11649 vcpu->arch.exception.pending = false; in __set_regs()
11650 vcpu->arch.exception_vmexit.pending = false; in __set_regs()
11657 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_regs()
11658 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_regs()
11659 return -EINVAL; in kvm_arch_vcpu_ioctl_set_regs()
11671 if (vcpu->arch.guest_state_protected) in __get_sregs_common()
11674 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __get_sregs_common()
11675 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __get_sregs_common()
11676 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __get_sregs_common()
11677 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __get_sregs_common()
11678 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __get_sregs_common()
11679 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __get_sregs_common()
11681 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __get_sregs_common()
11682 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __get_sregs_common()
11685 sregs->idt.limit = dt.size; in __get_sregs_common()
11686 sregs->idt.base = dt.address; in __get_sregs_common()
11688 sregs->gdt.limit = dt.size; in __get_sregs_common()
11689 sregs->gdt.base = dt.address; in __get_sregs_common()
11691 sregs->cr2 = vcpu->arch.cr2; in __get_sregs_common()
11692 sregs->cr3 = kvm_read_cr3(vcpu); in __get_sregs_common()
11695 sregs->cr0 = kvm_read_cr0(vcpu); in __get_sregs_common()
11696 sregs->cr4 = kvm_read_cr4(vcpu); in __get_sregs_common()
11697 sregs->cr8 = kvm_get_cr8(vcpu); in __get_sregs_common()
11698 sregs->efer = vcpu->arch.efer; in __get_sregs_common()
11699 sregs->apic_base = vcpu->arch.apic_base; in __get_sregs_common()
11706 if (vcpu->arch.guest_state_protected) in __get_sregs()
11709 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) in __get_sregs()
11710 set_bit(vcpu->arch.interrupt.nr, in __get_sregs()
11711 (unsigned long *)sregs->interrupt_bitmap); in __get_sregs()
11720 if (vcpu->arch.guest_state_protected) in __get_sregs2()
11725 sregs2->pdptrs[i] = kvm_pdptr_read(vcpu, i); in __get_sregs2()
11726 sregs2->flags |= KVM_SREGS2_FLAGS_PDPTRS_VALID; in __get_sregs2()
11733 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_get_sregs()
11734 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_get_sregs()
11735 return -EINVAL; in kvm_arch_vcpu_ioctl_get_sregs()
11757 if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || in kvm_arch_vcpu_ioctl_get_mpstate()
11758 vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && in kvm_arch_vcpu_ioctl_get_mpstate()
11759 vcpu->arch.pv.pv_unhalted) in kvm_arch_vcpu_ioctl_get_mpstate()
11760 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_ioctl_get_mpstate()
11762 mp_state->mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
11774 int ret = -EINVAL; in kvm_arch_vcpu_ioctl_set_mpstate()
11778 switch (mp_state->mp_state) { in kvm_arch_vcpu_ioctl_set_mpstate()
11801 if ((!kvm_apic_init_sipi_allowed(vcpu) || vcpu->arch.smi_pending) && in kvm_arch_vcpu_ioctl_set_mpstate()
11802 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || in kvm_arch_vcpu_ioctl_set_mpstate()
11803 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) in kvm_arch_vcpu_ioctl_set_mpstate()
11806 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { in kvm_arch_vcpu_ioctl_set_mpstate()
11807 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; in kvm_arch_vcpu_ioctl_set_mpstate()
11808 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); in kvm_arch_vcpu_ioctl_set_mpstate()
11810 vcpu->arch.mp_state = mp_state->mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
11822 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; in kvm_task_switch()
11834 if (ret || vcpu->mmio_needed) { in kvm_task_switch()
11835 vcpu->mmio_needed = false; in kvm_task_switch()
11836 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in kvm_task_switch()
11837 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; in kvm_task_switch()
11838 vcpu->run->internal.ndata = 0; in kvm_task_switch()
11842 kvm_rip_write(vcpu, ctxt->eip); in kvm_task_switch()
11843 kvm_set_rflags(vcpu, ctxt->eflags); in kvm_task_switch()
11850 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { in kvm_is_valid_sregs()
11853 * 64-bit mode (though maybe in a 32-bit code segment). in kvm_is_valid_sregs()
11856 if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA)) in kvm_is_valid_sregs()
11858 if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3)) in kvm_is_valid_sregs()
11862 * Not in 64-bit mode: EFER.LMA is clear and the code in kvm_is_valid_sregs()
11863 * segment cannot be 64-bit. in kvm_is_valid_sregs()
11865 if (sregs->efer & EFER_LMA || sregs->cs.l) in kvm_is_valid_sregs()
11869 return kvm_is_valid_cr4(vcpu, sregs->cr4) && in kvm_is_valid_sregs()
11870 kvm_is_valid_cr0(vcpu, sregs->cr0); in kvm_is_valid_sregs()
11880 return -EINVAL; in __set_sregs_common()
11882 if (kvm_apic_set_base(vcpu, sregs->apic_base, true)) in __set_sregs_common()
11883 return -EINVAL; in __set_sregs_common()
11885 if (vcpu->arch.guest_state_protected) in __set_sregs_common()
11888 dt.size = sregs->idt.limit; in __set_sregs_common()
11889 dt.address = sregs->idt.base; in __set_sregs_common()
11891 dt.size = sregs->gdt.limit; in __set_sregs_common()
11892 dt.address = sregs->gdt.base; in __set_sregs_common()
11895 vcpu->arch.cr2 = sregs->cr2; in __set_sregs_common()
11896 *mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; in __set_sregs_common()
11897 vcpu->arch.cr3 = sregs->cr3; in __set_sregs_common()
11899 kvm_x86_call(post_set_cr3)(vcpu, sregs->cr3); in __set_sregs_common()
11901 kvm_set_cr8(vcpu, sregs->cr8); in __set_sregs_common()
11903 *mmu_reset_needed |= vcpu->arch.efer != sregs->efer; in __set_sregs_common()
11904 kvm_x86_call(set_efer)(vcpu, sregs->efer); in __set_sregs_common()
11906 *mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; in __set_sregs_common()
11907 kvm_x86_call(set_cr0)(vcpu, sregs->cr0); in __set_sregs_common()
11909 *mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; in __set_sregs_common()
11910 kvm_x86_call(set_cr4)(vcpu, sregs->cr4); in __set_sregs_common()
11913 idx = srcu_read_lock(&vcpu->kvm->srcu); in __set_sregs_common()
11918 srcu_read_unlock(&vcpu->kvm->srcu, idx); in __set_sregs_common()
11921 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); in __set_sregs_common()
11922 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); in __set_sregs_common()
11923 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); in __set_sregs_common()
11924 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); in __set_sregs_common()
11925 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); in __set_sregs_common()
11926 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); in __set_sregs_common()
11928 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); in __set_sregs_common()
11929 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); in __set_sregs_common()
11935 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && in __set_sregs_common()
11937 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in __set_sregs_common()
11958 (const unsigned long *)sregs->interrupt_bitmap, max_bits); in __set_sregs()
11971 bool valid_pdptrs = sregs2->flags & KVM_SREGS2_FLAGS_PDPTRS_VALID; in __set_sregs2()
11972 bool pae = (sregs2->cr0 & X86_CR0_PG) && (sregs2->cr4 & X86_CR4_PAE) && in __set_sregs2()
11973 !(sregs2->efer & EFER_LMA); in __set_sregs2()
11976 if (sregs2->flags & ~KVM_SREGS2_FLAGS_PDPTRS_VALID) in __set_sregs2()
11977 return -EINVAL; in __set_sregs2()
11979 if (valid_pdptrs && (!pae || vcpu->arch.guest_state_protected)) in __set_sregs2()
11980 return -EINVAL; in __set_sregs2()
11989 kvm_pdptr_write(vcpu, i, sregs2->pdptrs[i]); in __set_sregs2()
11993 vcpu->arch.pdptrs_from_userspace = true; in __set_sregs2()
12007 if (vcpu->kvm->arch.has_protected_state && in kvm_arch_vcpu_ioctl_set_sregs()
12008 vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_sregs()
12009 return -EINVAL; in kvm_arch_vcpu_ioctl_set_sregs()
12026 down_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12029 if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) { in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12035 up_write(&kvm->arch.apicv_update_lock); in kvm_arch_vcpu_guestdbg_update_apicv_inhibit()
12044 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_ioctl_set_guest_debug()
12045 return -EINVAL; in kvm_arch_vcpu_ioctl_set_guest_debug()
12049 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12050 r = -EBUSY; in kvm_arch_vcpu_ioctl_set_guest_debug()
12053 if (dbg->control & KVM_GUESTDBG_INJECT_DB) in kvm_arch_vcpu_ioctl_set_guest_debug()
12065 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
12066 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) in kvm_arch_vcpu_ioctl_set_guest_debug()
12067 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
12069 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in kvm_arch_vcpu_ioctl_set_guest_debug()
12071 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12072 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12075 vcpu->arch.eff_db[i] = vcpu->arch.db[i]; in kvm_arch_vcpu_ioctl_set_guest_debug()
12079 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_arch_vcpu_ioctl_set_guest_debug()
12080 vcpu->arch.singlestep_rip = kvm_get_linear_rip(vcpu); in kvm_arch_vcpu_ioctl_set_guest_debug()
12090 kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm); in kvm_arch_vcpu_ioctl_set_guest_debug()
12105 unsigned long vaddr = tr->linear_address; in kvm_arch_vcpu_ioctl_translate()
12111 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_ioctl_translate()
12113 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_ioctl_translate()
12114 tr->physical_address = gpa; in kvm_arch_vcpu_ioctl_translate()
12115 tr->valid = gpa != INVALID_GPA; in kvm_arch_vcpu_ioctl_translate()
12116 tr->writeable = 1; in kvm_arch_vcpu_ioctl_translate()
12117 tr->usermode = 0; in kvm_arch_vcpu_ioctl_translate()
12127 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_get_fpu()
12128 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_get_fpu()
12132 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_get_fpu()
12133 memcpy(fpu->fpr, fxsave->st_space, 128); in kvm_arch_vcpu_ioctl_get_fpu()
12134 fpu->fcw = fxsave->cwd; in kvm_arch_vcpu_ioctl_get_fpu()
12135 fpu->fsw = fxsave->swd; in kvm_arch_vcpu_ioctl_get_fpu()
12136 fpu->ftwx = fxsave->twd; in kvm_arch_vcpu_ioctl_get_fpu()
12137 fpu->last_opcode = fxsave->fop; in kvm_arch_vcpu_ioctl_get_fpu()
12138 fpu->last_ip = fxsave->rip; in kvm_arch_vcpu_ioctl_get_fpu()
12139 fpu->last_dp = fxsave->rdp; in kvm_arch_vcpu_ioctl_get_fpu()
12140 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_get_fpu()
12150 if (fpstate_is_confidential(&vcpu->arch.guest_fpu)) in kvm_arch_vcpu_ioctl_set_fpu()
12151 return vcpu->kvm->arch.has_protected_state ? -EINVAL : 0; in kvm_arch_vcpu_ioctl_set_fpu()
12155 fxsave = &vcpu->arch.guest_fpu.fpstate->regs.fxsave; in kvm_arch_vcpu_ioctl_set_fpu()
12157 memcpy(fxsave->st_space, fpu->fpr, 128); in kvm_arch_vcpu_ioctl_set_fpu()
12158 fxsave->cwd = fpu->fcw; in kvm_arch_vcpu_ioctl_set_fpu()
12159 fxsave->swd = fpu->fsw; in kvm_arch_vcpu_ioctl_set_fpu()
12160 fxsave->twd = fpu->ftwx; in kvm_arch_vcpu_ioctl_set_fpu()
12161 fxsave->fop = fpu->last_opcode; in kvm_arch_vcpu_ioctl_set_fpu()
12162 fxsave->rip = fpu->last_ip; in kvm_arch_vcpu_ioctl_set_fpu()
12163 fxsave->rdp = fpu->last_dp; in kvm_arch_vcpu_ioctl_set_fpu()
12164 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); in kvm_arch_vcpu_ioctl_set_fpu()
12174 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) in store_regs()
12175 __get_regs(vcpu, &vcpu->run->s.regs.regs); in store_regs()
12177 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) in store_regs()
12178 __get_sregs(vcpu, &vcpu->run->s.regs.sregs); in store_regs()
12180 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) in store_regs()
12182 vcpu, &vcpu->run->s.regs.events); in store_regs()
12187 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { in sync_regs()
12188 __set_regs(vcpu, &vcpu->run->s.regs.regs); in sync_regs()
12189 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; in sync_regs()
12192 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { in sync_regs()
12193 struct kvm_sregs sregs = vcpu->run->s.regs.sregs; in sync_regs()
12196 return -EINVAL; in sync_regs()
12198 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; in sync_regs()
12201 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { in sync_regs()
12202 struct kvm_vcpu_events events = vcpu->run->s.regs.events; in sync_regs()
12205 return -EINVAL; in sync_regs()
12207 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; in sync_regs()
12215 if (kvm_check_tsc_unstable() && kvm->created_vcpus) in kvm_arch_vcpu_precreate()
12219 if (!kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12220 kvm->arch.max_vcpu_ids = KVM_MAX_VCPU_IDS; in kvm_arch_vcpu_precreate()
12222 if (id >= kvm->arch.max_vcpu_ids) in kvm_arch_vcpu_precreate()
12223 return -EINVAL; in kvm_arch_vcpu_precreate()
12233 vcpu->arch.last_vmentry_cpu = -1; in kvm_arch_vcpu_create()
12234 vcpu->arch.regs_avail = ~0; in kvm_arch_vcpu_create()
12235 vcpu->arch.regs_dirty = ~0; in kvm_arch_vcpu_create()
12237 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm); in kvm_arch_vcpu_create()
12239 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) in kvm_arch_vcpu_create()
12240 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_create()
12242 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; in kvm_arch_vcpu_create()
12252 r = -ENOMEM; in kvm_arch_vcpu_create()
12257 vcpu->arch.pio_data = page_address(page); in kvm_arch_vcpu_create()
12259 vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64), in kvm_arch_vcpu_create()
12261 vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64), in kvm_arch_vcpu_create()
12263 if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks) in kvm_arch_vcpu_create()
12265 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; in kvm_arch_vcpu_create()
12267 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, in kvm_arch_vcpu_create()
12274 if (!fpu_alloc_guest_fpstate(&vcpu->arch.guest_fpu)) { in kvm_arch_vcpu_create()
12279 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); in kvm_arch_vcpu_create()
12280 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); in kvm_arch_vcpu_create()
12284 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_STUFF_FEATURE_MSRS)) { in kvm_arch_vcpu_create()
12285 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); in kvm_arch_vcpu_create()
12286 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; in kvm_arch_vcpu_create()
12287 vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; in kvm_arch_vcpu_create()
12291 vcpu->arch.pending_external_vector = -1; in kvm_arch_vcpu_create()
12292 vcpu->arch.preempted_in_kernel = false; in kvm_arch_vcpu_create()
12295 vcpu->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_vcpu_create()
12304 kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); in kvm_arch_vcpu_create()
12311 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_create()
12313 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_create()
12315 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_create()
12317 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_create()
12318 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_create()
12319 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_create()
12329 struct kvm *kvm = vcpu->kvm; in kvm_arch_vcpu_postcreate()
12331 if (mutex_lock_killable(&vcpu->mutex)) in kvm_arch_vcpu_postcreate()
12338 vcpu->arch.msr_kvm_poll_control = 1; in kvm_arch_vcpu_postcreate()
12340 mutex_unlock(&vcpu->mutex); in kvm_arch_vcpu_postcreate()
12342 if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) in kvm_arch_vcpu_postcreate()
12343 schedule_delayed_work(&kvm->arch.kvmclock_sync_work, in kvm_arch_vcpu_postcreate()
12355 kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); in kvm_arch_vcpu_destroy()
12356 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); in kvm_arch_vcpu_destroy()
12357 fpu_free_guest_fpstate(&vcpu->arch.guest_fpu); in kvm_arch_vcpu_destroy()
12362 kfree(vcpu->arch.mce_banks); in kvm_arch_vcpu_destroy()
12363 kfree(vcpu->arch.mci_ctl2_banks); in kvm_arch_vcpu_destroy()
12365 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_arch_vcpu_destroy()
12367 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_arch_vcpu_destroy()
12368 free_page((unsigned long)vcpu->arch.pio_data); in kvm_arch_vcpu_destroy()
12369 kvfree(vcpu->arch.cpuid_entries); in kvm_arch_vcpu_destroy()
12379 * Several of the "set" flows, e.g. ->set_cr0(), read other registers in kvm_vcpu_reset()
12389 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's in kvm_vcpu_reset()
12400 vcpu->arch.hflags = 0; in kvm_vcpu_reset()
12402 vcpu->arch.smi_pending = 0; in kvm_vcpu_reset()
12403 vcpu->arch.smi_count = 0; in kvm_vcpu_reset()
12404 atomic_set(&vcpu->arch.nmi_queued, 0); in kvm_vcpu_reset()
12405 vcpu->arch.nmi_pending = 0; in kvm_vcpu_reset()
12406 vcpu->arch.nmi_injected = false; in kvm_vcpu_reset()
12410 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); in kvm_vcpu_reset()
12412 vcpu->arch.dr6 = DR6_ACTIVE_LOW; in kvm_vcpu_reset()
12413 vcpu->arch.dr7 = DR7_FIXED_1; in kvm_vcpu_reset()
12416 vcpu->arch.cr2 = 0; in kvm_vcpu_reset()
12419 vcpu->arch.apf.msr_en_val = 0; in kvm_vcpu_reset()
12420 vcpu->arch.apf.msr_int_val = 0; in kvm_vcpu_reset()
12421 vcpu->arch.st.msr_val = 0; in kvm_vcpu_reset()
12427 vcpu->arch.apf.halted = false; in kvm_vcpu_reset()
12429 if (vcpu->arch.guest_fpu.fpstate && kvm_mpx_supported()) { in kvm_vcpu_reset()
12430 struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate; in kvm_vcpu_reset()
12447 vcpu->arch.smbase = 0x30000; in kvm_vcpu_reset()
12449 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; in kvm_vcpu_reset()
12451 vcpu->arch.msr_misc_features_enables = 0; in kvm_vcpu_reset()
12452 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | in kvm_vcpu_reset()
12460 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); in kvm_vcpu_reset()
12471 kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600); in kvm_vcpu_reset()
12478 vcpu->arch.cr3 = 0; in kvm_vcpu_reset()
12500 * which PCIDs have to be flushed. However, CR0.WP and the paging-related in kvm_vcpu_reset()
12570 if (!stable && vcpu->cpu == smp_processor_id()) in kvm_arch_enable_virtualization_cpu()
12572 if (stable && vcpu->arch.last_host_tsc > local_tsc) { in kvm_arch_enable_virtualization_cpu()
12574 if (vcpu->arch.last_host_tsc > max_tsc) in kvm_arch_enable_virtualization_cpu()
12575 max_tsc = vcpu->arch.last_host_tsc; in kvm_arch_enable_virtualization_cpu()
12605 * N.B. - this code below runs only on platforms with reliable TSC, in kvm_arch_enable_virtualization_cpu()
12619 u64 delta_cyc = max_tsc - local_tsc; in kvm_arch_enable_virtualization_cpu()
12621 kvm->arch.backwards_tsc_observed = true; in kvm_arch_enable_virtualization_cpu()
12623 vcpu->arch.tsc_offset_adjustment += delta_cyc; in kvm_arch_enable_virtualization_cpu()
12624 vcpu->arch.last_host_tsc = local_tsc; in kvm_arch_enable_virtualization_cpu()
12634 kvm->arch.last_tsc_nsec = 0; in kvm_arch_enable_virtualization_cpu()
12635 kvm->arch.last_tsc_write = 0; in kvm_arch_enable_virtualization_cpu()
12650 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; in kvm_vcpu_is_reset_bsp()
12655 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; in kvm_vcpu_is_bsp()
12661 kfree(kvm->arch.hv_pa_pg); in kvm_arch_free_vm()
12673 return -EINVAL; in kvm_arch_init_vm()
12675 kvm->arch.vm_type = type; in kvm_arch_init_vm()
12676 kvm->arch.has_private_mem = in kvm_arch_init_vm()
12679 kvm->arch.pre_fault_allowed = in kvm_arch_init_vm()
12692 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); in kvm_arch_init_vm()
12693 atomic_set(&kvm->arch.noncoherent_dma_count, 0); in kvm_arch_init_vm()
12696 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12697 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ in kvm_arch_init_vm()
12699 &kvm->arch.irq_sources_bitmap); in kvm_arch_init_vm()
12701 raw_spin_lock_init(&kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12702 mutex_init(&kvm->arch.apic_map_lock); in kvm_arch_init_vm()
12703 seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lock); in kvm_arch_init_vm()
12704 kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); in kvm_arch_init_vm()
12706 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12708 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); in kvm_arch_init_vm()
12710 kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; in kvm_arch_init_vm()
12711 kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT; in kvm_arch_init_vm()
12712 kvm->arch.guest_can_read_msr_platform_info = true; in kvm_arch_init_vm()
12713 kvm->arch.enable_pmu = enable_pmu; in kvm_arch_init_vm()
12716 spin_lock_init(&kvm->arch.hv_root_tdp_lock); in kvm_arch_init_vm()
12717 kvm->arch.hv_root_tdp = INVALID_PAGE; in kvm_arch_init_vm()
12720 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); in kvm_arch_init_vm()
12721 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); in kvm_arch_init_vm()
12768 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); in kvm_arch_sync_events()
12769 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); in kvm_arch_sync_events()
12786 * -errno: on error
12791 * GPA->HVA translation will not change. However, the HVA is a user
12803 /* Called with kvm->slots_lock held. */ in __x86_set_memory_region()
12805 return ERR_PTR_USR(-EINVAL); in __x86_set_memory_region()
12809 if (slot && slot->npages) in __x86_set_memory_region()
12810 return ERR_PTR_USR(-EEXIST); in __x86_set_memory_region()
12821 if (!slot || !slot->npages) in __x86_set_memory_region()
12824 old_npages = slot->npages; in __x86_set_memory_region()
12825 hva = slot->userspace_addr; in __x86_set_memory_region()
12855 if (current->mm == kvm->mm) { in kvm_arch_destroy_vm()
12861 mutex_lock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12867 mutex_unlock(&kvm->slots_lock); in kvm_arch_destroy_vm()
12871 kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12875 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); in kvm_arch_destroy_vm()
12876 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); in kvm_arch_destroy_vm()
12888 vfree(slot->arch.rmap[i]); in memslot_rmap_free()
12889 slot->arch.rmap[i] = NULL; in memslot_rmap_free()
12900 vfree(slot->arch.lpage_info[i - 1]); in kvm_arch_free_memslot()
12901 slot->arch.lpage_info[i - 1] = NULL; in kvm_arch_free_memslot()
12909 const int sz = sizeof(*slot->arch.rmap[0]); in memslot_rmap_alloc()
12916 if (slot->arch.rmap[i]) in memslot_rmap_alloc()
12919 slot->arch.rmap[i] = __vcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); in memslot_rmap_alloc()
12920 if (!slot->arch.rmap[i]) { in memslot_rmap_alloc()
12922 return -ENOMEM; in memslot_rmap_alloc()
12932 unsigned long npages = slot->npages; in kvm_alloc_memslot_metadata()
12940 memset(&slot->arch, 0, sizeof(slot->arch)); in kvm_alloc_memslot_metadata()
12960 slot->arch.lpage_info[i - 1] = linfo; in kvm_alloc_memslot_metadata()
12962 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12964 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) in kvm_alloc_memslot_metadata()
12965 linfo[lpages - 1].disallow_lpage = 1; in kvm_alloc_memslot_metadata()
12966 ugfn = slot->userspace_addr >> PAGE_SHIFT; in kvm_alloc_memslot_metadata()
12971 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { in kvm_alloc_memslot_metadata()
12992 vfree(slot->arch.lpage_info[i - 1]); in kvm_alloc_memslot_metadata()
12993 slot->arch.lpage_info[i - 1] = NULL; in kvm_alloc_memslot_metadata()
12995 return -ENOMEM; in kvm_alloc_memslot_metadata()
13004 * memslots->generation has been incremented. in kvm_arch_memslots_updated()
13009 /* Force re-initialization of steal_time cache */ in kvm_arch_memslots_updated()
13024 return -EINVAL; in kvm_arch_prepare_memory_region()
13027 if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) in kvm_arch_prepare_memory_region()
13028 return -EINVAL; in kvm_arch_prepare_memory_region()
13034 memcpy(&new->arch, &old->arch, sizeof(old->arch)); in kvm_arch_prepare_memory_region()
13036 return -EIO; in kvm_arch_prepare_memory_region()
13049 nr_slots = atomic_read(&kvm->nr_memslots_dirty_logging); in kvm_mmu_update_cpu_dirty_logging()
13059 u32 old_flags = old ? old->flags : 0; in kvm_mmu_slot_apply_flags()
13060 u32 new_flags = new ? new->flags : 0; in kvm_mmu_slot_apply_flags()
13080 * CREATE: No shadow pages exist, thus nothing to write-protect in kvm_mmu_slot_apply_flags()
13089 * READONLY and non-flags changes were filtered out above, and the only in kvm_mmu_slot_apply_flags()
13109 * Initially-all-set does not require write protecting any page, in kvm_mmu_slot_apply_flags()
13134 * write-protected before returning to userspace, i.e. before in kvm_mmu_slot_apply_flags()
13141 * Specifically, KVM also write-protects guest page tables to in kvm_mmu_slot_apply_flags()
13150 * To handle these scenarios, KVM uses a separate software-only in kvm_mmu_slot_apply_flags()
13151 * bit (MMU-writable) to track if a SPTE is !writable due to in kvm_mmu_slot_apply_flags()
13152 * a guest page table being write-protected (KVM clears the in kvm_mmu_slot_apply_flags()
13153 * MMU-writable flag when write-protecting for shadow paging). in kvm_mmu_slot_apply_flags()
13155 * The use of MMU-writable is also the primary motivation for in kvm_mmu_slot_apply_flags()
13158 * !MMU-writable SPTE, KVM must flush if it encounters any in kvm_mmu_slot_apply_flags()
13159 * MMU-writable SPTE regardless of whether the actual hardware in kvm_mmu_slot_apply_flags()
13162 * write access" helpers to ignore MMU-writable entirely. in kvm_mmu_slot_apply_flags()
13165 * access-tracked SPTEs is particularly relevant). in kvm_mmu_slot_apply_flags()
13179 if (!kvm->arch.n_requested_mmu_pages && in kvm_arch_commit_memory_region()
13183 nr_mmu_pages = kvm->nr_memslot_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; in kvm_arch_commit_memory_region()
13199 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_in_kernel()
13209 if (vcpu->arch.guest_state_protected) in kvm_arch_vcpu_get_ip()
13228 if (vcpu->arch.guest_state_protected) in kvm_get_linear_rip()
13249 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in kvm_get_rflags()
13257 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && in __kvm_set_rflags()
13258 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) in __kvm_set_rflags()
13279 return (key + 1) & (ASYNC_PF_PER_VCPU - 1); in kvm_async_pf_next_probe()
13286 while (vcpu->arch.apf.gfns[key] != ~0) in kvm_add_async_pf_gfn()
13289 vcpu->arch.apf.gfns[key] = gfn; in kvm_add_async_pf_gfn()
13298 (vcpu->arch.apf.gfns[key] != gfn && in kvm_async_pf_gfn_slot()
13299 vcpu->arch.apf.gfns[key] != ~0); i++) in kvm_async_pf_gfn_slot()
13307 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; in kvm_find_async_pf_gfn()
13316 if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) in kvm_del_async_pf_gfn()
13320 vcpu->arch.apf.gfns[i] = ~0; in kvm_del_async_pf_gfn()
13323 if (vcpu->arch.apf.gfns[j] == ~0) in kvm_del_async_pf_gfn()
13325 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); in kvm_del_async_pf_gfn()
13332 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; in kvm_del_async_pf_gfn()
13341 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, in apf_put_user_notpresent()
13349 return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_put_user_ready()
13358 if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, in apf_pageready_slot_free()
13371 if (vcpu->arch.apf.send_user_only && in kvm_can_deliver_async_pf()
13380 return vcpu->arch.apf.delivery_as_pf_vmexit; in kvm_can_deliver_async_pf()
13398 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) in kvm_can_do_async_pf()
13413 trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_not_present()
13414 kvm_add_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_not_present()
13422 fault.address = work->arch.token; in kvm_arch_async_page_not_present()
13445 .vector = vcpu->arch.apf.vec in kvm_arch_async_page_present()
13448 if (work->wakeup_all) in kvm_arch_async_page_present()
13449 work->arch.token = ~0; /* broadcast wakeup */ in kvm_arch_async_page_present()
13451 kvm_del_async_pf_gfn(vcpu, work->arch.gfn); in kvm_arch_async_page_present()
13452 trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); in kvm_arch_async_page_present()
13454 if ((work->wakeup_all || work->notpresent_injected) && in kvm_arch_async_page_present()
13456 !apf_put_user_ready(vcpu, work->arch.token)) { in kvm_arch_async_page_present()
13457 vcpu->arch.apf.pageready_pending = true; in kvm_arch_async_page_present()
13461 vcpu->arch.apf.halted = false; in kvm_arch_async_page_present()
13462 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; in kvm_arch_async_page_present()
13468 if (!vcpu->arch.apf.pageready_pending) in kvm_arch_async_page_present_queued()
13482 if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) in kvm_arch_start_assignment()
13489 atomic_dec(&kvm->arch.assigned_device_count); in kvm_arch_end_assignment()
13495 return raw_atomic_read(&kvm->arch.assigned_device_count); in kvm_arch_has_assigned_device()
13502 * Non-coherent DMA assignment and de-assignment may affect whether or in kvm_noncoherent_dma_assignment_start_or_stop()
13505 * (or last) non-coherent device is (un)registered to so that new SPTEs in kvm_noncoherent_dma_assignment_start_or_stop()
13514 if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) in kvm_arch_register_noncoherent_dma()
13521 if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) in kvm_arch_unregister_noncoherent_dma()
13528 return atomic_read(&kvm->arch.noncoherent_dma_count); in kvm_arch_has_noncoherent_dma()
13544 irqfd->producer = prod; in kvm_arch_irq_bypass_add_producer()
13545 kvm_arch_start_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13546 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_add_producer()
13547 prod->irq, irqfd->gsi, 1); in kvm_arch_irq_bypass_add_producer()
13549 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_add_producer()
13561 WARN_ON(irqfd->producer != prod); in kvm_arch_irq_bypass_del_producer()
13562 irqfd->producer = NULL; in kvm_arch_irq_bypass_del_producer()
13566 * remapped mode, so we can re-use the current implementation in kvm_arch_irq_bypass_del_producer()
13570 ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, in kvm_arch_irq_bypass_del_producer()
13571 prod->irq, irqfd->gsi, 0); in kvm_arch_irq_bypass_del_producer()
13574 " fails: %d\n", irqfd->consumer.token, ret); in kvm_arch_irq_bypass_del_producer()
13576 kvm_arch_end_assignment(irqfd->kvm); in kvm_arch_irq_bypass_del_producer()
13588 if (new->type != KVM_IRQ_ROUTING_MSI) in kvm_arch_irqfd_route_changed()
13591 return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); in kvm_arch_irqfd_route_changed()
13601 return (vcpu->arch.msr_kvm_poll_control & 1) == 0; in kvm_arch_no_poll()
13647 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in kvm_fixup_and_inject_pf_error()
13653 mmu->gva_to_gpa(vcpu, mmu, gva, access, &fault) != INVALID_GPA) { in kvm_fixup_and_inject_pf_error()
13655 * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page in kvm_fixup_and_inject_pf_error()
13666 vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); in kvm_fixup_and_inject_pf_error()
13679 if (KVM_BUG_ON(!e, vcpu->kvm)) in kvm_handle_memory_failure()
13680 return -EIO; in kvm_handle_memory_failure()
13690 * doesn't seem to be a real use-case behind such requests, just return in kvm_handle_memory_failure()
13746 * page tables, so a non-global flush just degenerates to a in kvm_handle_invpcid()
13765 struct kvm_run *run = vcpu->run; in complete_sev_es_emulated_mmio()
13769 BUG_ON(!vcpu->mmio_needed); in complete_sev_es_emulated_mmio()
13772 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; in complete_sev_es_emulated_mmio()
13773 len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13774 if (!vcpu->mmio_is_write) in complete_sev_es_emulated_mmio()
13775 memcpy(frag->data, run->mmio.data, len); in complete_sev_es_emulated_mmio()
13777 if (frag->len <= 8) { in complete_sev_es_emulated_mmio()
13780 vcpu->mmio_cur_fragment++; in complete_sev_es_emulated_mmio()
13783 frag->data += len; in complete_sev_es_emulated_mmio()
13784 frag->gpa += len; in complete_sev_es_emulated_mmio()
13785 frag->len -= len; in complete_sev_es_emulated_mmio()
13788 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { in complete_sev_es_emulated_mmio()
13789 vcpu->mmio_needed = 0; in complete_sev_es_emulated_mmio()
13797 run->mmio.phys_addr = frag->gpa; in complete_sev_es_emulated_mmio()
13798 run->mmio.len = min(8u, frag->len); in complete_sev_es_emulated_mmio()
13799 run->mmio.is_write = vcpu->mmio_is_write; in complete_sev_es_emulated_mmio()
13800 if (run->mmio.is_write) in complete_sev_es_emulated_mmio()
13801 memcpy(run->mmio.data, frag->data, min(8u, frag->len)); in complete_sev_es_emulated_mmio()
13802 run->exit_reason = KVM_EXIT_MMIO; in complete_sev_es_emulated_mmio()
13804 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in complete_sev_es_emulated_mmio()
13816 return -EINVAL; in kvm_sev_es_mmio_write()
13822 bytes -= handled; in kvm_sev_es_mmio_write()
13827 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_write()
13828 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_write()
13829 frag->len = bytes; in kvm_sev_es_mmio_write()
13830 frag->gpa = gpa; in kvm_sev_es_mmio_write()
13831 frag->data = data; in kvm_sev_es_mmio_write()
13833 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_write()
13834 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_write()
13836 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_write()
13837 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_write()
13838 vcpu->run->mmio.is_write = 1; in kvm_sev_es_mmio_write()
13839 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); in kvm_sev_es_mmio_write()
13840 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_write()
13842 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_write()
13855 return -EINVAL; in kvm_sev_es_mmio_read()
13861 bytes -= handled; in kvm_sev_es_mmio_read()
13866 frag = vcpu->mmio_fragments; in kvm_sev_es_mmio_read()
13867 vcpu->mmio_nr_fragments = 1; in kvm_sev_es_mmio_read()
13868 frag->len = bytes; in kvm_sev_es_mmio_read()
13869 frag->gpa = gpa; in kvm_sev_es_mmio_read()
13870 frag->data = data; in kvm_sev_es_mmio_read()
13872 vcpu->mmio_needed = 1; in kvm_sev_es_mmio_read()
13873 vcpu->mmio_cur_fragment = 0; in kvm_sev_es_mmio_read()
13875 vcpu->run->mmio.phys_addr = gpa; in kvm_sev_es_mmio_read()
13876 vcpu->run->mmio.len = min(8u, frag->len); in kvm_sev_es_mmio_read()
13877 vcpu->run->mmio.is_write = 0; in kvm_sev_es_mmio_read()
13878 vcpu->run->exit_reason = KVM_EXIT_MMIO; in kvm_sev_es_mmio_read()
13880 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_mmio; in kvm_sev_es_mmio_read()
13888 vcpu->arch.sev_pio_count -= count; in advance_sev_es_emulated_pio()
13889 vcpu->arch.sev_pio_data += count * size; in advance_sev_es_emulated_pio()
13897 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_outs()
13898 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_outs()
13900 vcpu->arch.pio.count = 0; in complete_sev_es_emulated_outs()
13901 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_outs()
13911 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_outs()
13912 int ret = emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, count); in kvm_sev_es_outs()
13920 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_outs()
13924 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs; in kvm_sev_es_outs()
13933 unsigned count = vcpu->arch.pio.count; in complete_sev_es_emulated_ins()
13934 int size = vcpu->arch.pio.size; in complete_sev_es_emulated_ins()
13935 int port = vcpu->arch.pio.port; in complete_sev_es_emulated_ins()
13937 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); in complete_sev_es_emulated_ins()
13939 if (vcpu->arch.sev_pio_count) in complete_sev_es_emulated_ins()
13949 min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); in kvm_sev_es_ins()
13950 if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) in kvm_sev_es_ins()
13955 if (!vcpu->arch.sev_pio_count) in kvm_sev_es_ins()
13959 vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; in kvm_sev_es_ins()
13967 vcpu->arch.sev_pio_data = data; in kvm_sev_es_string_io()
13968 vcpu->arch.sev_pio_count = count; in kvm_sev_es_string_io()