Lines Matching full:vcpu

19 	STATS_DESC_COUNTER(VCPU, int_exits),
20 STATS_DESC_COUNTER(VCPU, idle_exits),
21 STATS_DESC_COUNTER(VCPU, cpucfg_exits),
22 STATS_DESC_COUNTER(VCPU, signal_exits),
23 STATS_DESC_COUNTER(VCPU, hypercall_exits)
35 static inline void kvm_save_host_pmu(struct kvm_vcpu *vcpu) in kvm_save_host_pmu() argument
39 context = this_cpu_ptr(vcpu->kvm->arch.vmcs); in kvm_save_host_pmu()
50 static inline void kvm_restore_host_pmu(struct kvm_vcpu *vcpu) in kvm_restore_host_pmu() argument
54 context = this_cpu_ptr(vcpu->kvm->arch.vmcs); in kvm_restore_host_pmu()
66 static inline void kvm_save_guest_pmu(struct kvm_vcpu *vcpu) in kvm_save_guest_pmu() argument
68 struct loongarch_csrs *csr = vcpu->arch.csr; in kvm_save_guest_pmu()
80 static inline void kvm_restore_guest_pmu(struct kvm_vcpu *vcpu) in kvm_restore_guest_pmu() argument
82 struct loongarch_csrs *csr = vcpu->arch.csr; in kvm_restore_guest_pmu()
94 static int kvm_own_pmu(struct kvm_vcpu *vcpu) in kvm_own_pmu() argument
98 if (!kvm_guest_has_pmu(&vcpu->arch)) in kvm_own_pmu()
101 kvm_save_host_pmu(vcpu); in kvm_own_pmu()
105 val |= (kvm_get_pmu_num(&vcpu->arch) + 1) << CSR_GCFG_GPERF_SHIFT; in kvm_own_pmu()
108 kvm_restore_guest_pmu(vcpu); in kvm_own_pmu()
113 static void kvm_lose_pmu(struct kvm_vcpu *vcpu) in kvm_lose_pmu() argument
116 struct loongarch_csrs *csr = vcpu->arch.csr; in kvm_lose_pmu()
118 if (!(vcpu->arch.aux_inuse & KVM_LARCH_PMU)) in kvm_lose_pmu()
121 kvm_save_guest_pmu(vcpu); in kvm_lose_pmu()
136 vcpu->arch.aux_inuse &= ~KVM_LARCH_PMU; in kvm_lose_pmu()
138 kvm_restore_host_pmu(vcpu); in kvm_lose_pmu()
141 static void kvm_restore_pmu(struct kvm_vcpu *vcpu) in kvm_restore_pmu() argument
143 if ((vcpu->arch.aux_inuse & KVM_LARCH_PMU)) in kvm_restore_pmu()
144 kvm_make_request(KVM_REQ_PMU, vcpu); in kvm_restore_pmu()
147 static void kvm_check_pmu(struct kvm_vcpu *vcpu) in kvm_check_pmu() argument
149 if (kvm_check_request(KVM_REQ_PMU, vcpu)) { in kvm_check_pmu()
150 kvm_own_pmu(vcpu); in kvm_check_pmu()
151 vcpu->arch.aux_inuse |= KVM_LARCH_PMU; in kvm_check_pmu()
155 static void kvm_update_stolen_time(struct kvm_vcpu *vcpu) in kvm_update_stolen_time() argument
164 ghc = &vcpu->arch.st.cache; in kvm_update_stolen_time()
165 gpa = vcpu->arch.st.guest_addr; in kvm_update_stolen_time()
170 slots = kvm_memslots(vcpu->kvm); in kvm_update_stolen_time()
172 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st))) { in kvm_update_stolen_time()
188 steal += current->sched_info.run_delay - vcpu->arch.st.last_steal; in kvm_update_stolen_time()
189 vcpu->arch.st.last_steal = current->sched_info.run_delay; in kvm_update_stolen_time()
196 mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); in kvm_update_stolen_time()
200 * kvm_check_requests - check and handle pending vCPU requests
205 static int kvm_check_requests(struct kvm_vcpu *vcpu) in kvm_check_requests() argument
207 if (!kvm_request_pending(vcpu)) in kvm_check_requests()
210 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) in kvm_check_requests()
211 vcpu->arch.vpid = 0; /* Drop vpid for this vCPU */ in kvm_check_requests()
213 if (kvm_dirty_ring_check_request(vcpu)) in kvm_check_requests()
216 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) in kvm_check_requests()
217 kvm_update_stolen_time(vcpu); in kvm_check_requests()
222 static void kvm_late_check_requests(struct kvm_vcpu *vcpu) in kvm_late_check_requests() argument
225 if (kvm_check_request(KVM_REQ_TLB_FLUSH_GPA, vcpu)) in kvm_late_check_requests()
226 if (vcpu->arch.flush_gpa != INVALID_GPA) { in kvm_late_check_requests()
227 kvm_flush_tlb_gpa(vcpu, vcpu->arch.flush_gpa); in kvm_late_check_requests()
228 vcpu->arch.flush_gpa = INVALID_GPA; in kvm_late_check_requests()
233 * Check and handle pending signal and vCPU requests etc
241 static int kvm_enter_guest_check(struct kvm_vcpu *vcpu) in kvm_enter_guest_check() argument
248 ret = xfer_to_guest_mode_handle_work(vcpu); in kvm_enter_guest_check()
252 idx = srcu_read_lock(&vcpu->kvm->srcu); in kvm_enter_guest_check()
253 ret = kvm_check_requests(vcpu); in kvm_enter_guest_check()
254 srcu_read_unlock(&vcpu->kvm->srcu, idx); in kvm_enter_guest_check()
265 static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu) in kvm_pre_enter_guest() argument
270 ret = kvm_enter_guest_check(vcpu); in kvm_pre_enter_guest()
275 * Handle vcpu timer, interrupts, check requests and in kvm_pre_enter_guest()
276 * check vmid before vcpu enter guest in kvm_pre_enter_guest()
279 kvm_deliver_intr(vcpu); in kvm_pre_enter_guest()
280 kvm_deliver_exception(vcpu); in kvm_pre_enter_guest()
281 /* Make sure the vcpu mode has been written */ in kvm_pre_enter_guest()
282 smp_store_mb(vcpu->mode, IN_GUEST_MODE); in kvm_pre_enter_guest()
283 kvm_check_vpid(vcpu); in kvm_pre_enter_guest()
284 kvm_check_pmu(vcpu); in kvm_pre_enter_guest()
291 kvm_late_check_requests(vcpu); in kvm_pre_enter_guest()
292 vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY); in kvm_pre_enter_guest()
294 vcpu->arch.aux_inuse &= ~KVM_LARCH_SWCSR_LATEST; in kvm_pre_enter_guest()
296 if (kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending()) { in kvm_pre_enter_guest()
297 /* make sure the vcpu mode has been written */ in kvm_pre_enter_guest()
298 smp_store_mb(vcpu->mode, OUTSIDE_GUEST_MODE); in kvm_pre_enter_guest()
310 static int kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) in kvm_handle_exit() argument
313 unsigned long estat = vcpu->arch.host_estat; in kvm_handle_exit()
317 vcpu->mode = OUTSIDE_GUEST_MODE; in kvm_handle_exit()
322 kvm_lose_pmu(vcpu); in kvm_handle_exit()
328 trace_kvm_exit(vcpu, ecode); in kvm_handle_exit()
330 ret = kvm_handle_fault(vcpu, ecode); in kvm_handle_exit()
333 ++vcpu->stat.int_exits; in kvm_handle_exit()
337 ret = kvm_pre_enter_guest(vcpu); in kvm_handle_exit()
346 trace_kvm_reenter(vcpu); in kvm_handle_exit()
351 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_runnable() argument
353 return !!(vcpu->arch.irq_pending) && in kvm_arch_vcpu_runnable()
354 vcpu->arch.mp_state.mp_state == KVM_MP_STATE_RUNNABLE; in kvm_arch_vcpu_runnable()
357 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_should_kick() argument
359 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; in kvm_arch_vcpu_should_kick()
362 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_in_kernel() argument
367 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) in kvm_arch_vcpu_fault() argument
372 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, in kvm_arch_vcpu_ioctl_translate() argument
378 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) in kvm_cpu_has_pending_timer() argument
384 ret = kvm_pending_timer(vcpu) || in kvm_cpu_has_pending_timer()
391 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_dump_regs() argument
395 kvm_debug("vCPU Register Dump:\n"); in kvm_arch_vcpu_dump_regs()
396 kvm_debug("\tPC = 0x%08lx\n", vcpu->arch.pc); in kvm_arch_vcpu_dump_regs()
397 kvm_debug("\tExceptions: %08lx\n", vcpu->arch.irq_pending); in kvm_arch_vcpu_dump_regs()
401 vcpu->arch.gprs[i], vcpu->arch.gprs[i + 1], in kvm_arch_vcpu_dump_regs()
402 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]); in kvm_arch_vcpu_dump_regs()
414 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, in kvm_arch_vcpu_ioctl_get_mpstate() argument
417 *mp_state = vcpu->arch.mp_state; in kvm_arch_vcpu_ioctl_get_mpstate()
422 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, in kvm_arch_vcpu_ioctl_set_mpstate() argument
429 vcpu->arch.mp_state = *mp_state; in kvm_arch_vcpu_ioctl_set_mpstate()
438 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, in kvm_arch_vcpu_ioctl_set_guest_debug() argument
445 vcpu->guest_debug = dbg->control; in kvm_arch_vcpu_ioctl_set_guest_debug()
447 vcpu->guest_debug = 0; in kvm_arch_vcpu_ioctl_set_guest_debug()
452 static inline int kvm_set_cpuid(struct kvm_vcpu *vcpu, u64 val) in kvm_set_cpuid() argument
456 struct loongarch_csrs *csr = vcpu->arch.csr; in kvm_set_cpuid()
461 map = vcpu->kvm->arch.phyid_map; in kvm_set_cpuid()
464 spin_lock(&vcpu->kvm->arch.phyid_map_lock); in kvm_set_cpuid()
468 spin_unlock(&vcpu->kvm->arch.phyid_map_lock); in kvm_set_cpuid()
476 spin_unlock(&vcpu->kvm->arch.phyid_map_lock); in kvm_set_cpuid()
482 if (vcpu == map->phys_map[val].vcpu) { in kvm_set_cpuid()
483 spin_unlock(&vcpu->kvm->arch.phyid_map_lock); in kvm_set_cpuid()
488 * New CPUID is already set with other vcpu in kvm_set_cpuid()
491 spin_unlock(&vcpu->kvm->arch.phyid_map_lock); in kvm_set_cpuid()
497 map->phys_map[val].vcpu = vcpu; in kvm_set_cpuid()
498 spin_unlock(&vcpu->kvm->arch.phyid_map_lock); in kvm_set_cpuid()
503 static inline void kvm_drop_cpuid(struct kvm_vcpu *vcpu) in kvm_drop_cpuid() argument
507 struct loongarch_csrs *csr = vcpu->arch.csr; in kvm_drop_cpuid()
509 map = vcpu->kvm->arch.phyid_map; in kvm_drop_cpuid()
515 spin_lock(&vcpu->kvm->arch.phyid_map_lock); in kvm_drop_cpuid()
517 map->phys_map[cpuid].vcpu = NULL; in kvm_drop_cpuid()
521 spin_unlock(&vcpu->kvm->arch.phyid_map_lock); in kvm_drop_cpuid()
535 return map->phys_map[cpuid].vcpu; in kvm_get_vcpu_by_cpuid()
538 static int _kvm_getcsr(struct kvm_vcpu *vcpu, unsigned int id, u64 *val) in _kvm_getcsr() argument
541 struct loongarch_csrs *csr = vcpu->arch.csr; in _kvm_getcsr()
548 vcpu_load(vcpu); in _kvm_getcsr()
553 kvm_deliver_intr(vcpu); in _kvm_getcsr()
554 vcpu->arch.aux_inuse &= ~KVM_LARCH_SWCSR_LATEST; in _kvm_getcsr()
555 vcpu_put(vcpu); in _kvm_getcsr()
573 static int _kvm_setcsr(struct kvm_vcpu *vcpu, unsigned int id, u64 val) in _kvm_setcsr() argument
576 struct loongarch_csrs *csr = vcpu->arch.csr; in _kvm_setcsr()
582 return kvm_set_cpuid(vcpu, val); in _kvm_setcsr()
598 * After modifying the PMU CSR register value of the vcpu. in _kvm_setcsr()
610 kvm_make_request(KVM_REQ_PMU, vcpu); in _kvm_setcsr()
727 static int kvm_get_one_reg(struct kvm_vcpu *vcpu, in kvm_get_one_reg() argument
736 ret = _kvm_getcsr(vcpu, id, v); in kvm_get_one_reg()
741 *v = vcpu->arch.cpucfg[id]; in kvm_get_one_reg()
746 if (!kvm_guest_has_lbt(&vcpu->arch)) in kvm_get_one_reg()
751 *v = vcpu->arch.lbt.scr0; in kvm_get_one_reg()
754 *v = vcpu->arch.lbt.scr1; in kvm_get_one_reg()
757 *v = vcpu->arch.lbt.scr2; in kvm_get_one_reg()
760 *v = vcpu->arch.lbt.scr3; in kvm_get_one_reg()
763 *v = vcpu->arch.lbt.eflags; in kvm_get_one_reg()
766 *v = vcpu->arch.fpu.ftop; in kvm_get_one_reg()
776 *v = drdtime() + vcpu->kvm->arch.time_offset; in kvm_get_one_reg()
794 static int kvm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) in kvm_get_reg() argument
801 ret = kvm_get_one_reg(vcpu, reg, &v); in kvm_get_reg()
814 static int kvm_set_one_reg(struct kvm_vcpu *vcpu, in kvm_set_one_reg() argument
823 ret = _kvm_setcsr(vcpu, id, v); in kvm_set_one_reg()
830 vcpu->arch.cpucfg[id] = (u32)v; in kvm_set_one_reg()
832 vcpu->arch.max_pmu_csrid = in kvm_set_one_reg()
833 LOONGARCH_CSR_PERFCTRL0 + 2 * kvm_get_pmu_num(&vcpu->arch) + 1; in kvm_set_one_reg()
836 if (!kvm_guest_has_lbt(&vcpu->arch)) in kvm_set_one_reg()
841 vcpu->arch.lbt.scr0 = v; in kvm_set_one_reg()
844 vcpu->arch.lbt.scr1 = v; in kvm_set_one_reg()
847 vcpu->arch.lbt.scr2 = v; in kvm_set_one_reg()
850 vcpu->arch.lbt.scr3 = v; in kvm_set_one_reg()
853 vcpu->arch.lbt.eflags = v; in kvm_set_one_reg()
856 vcpu->arch.fpu.ftop = v; in kvm_set_one_reg()
867 * gftoffset is relative with board, not vcpu in kvm_set_one_reg()
870 if (vcpu->vcpu_id == 0) in kvm_set_one_reg()
871 vcpu->kvm->arch.time_offset = (signed long)(v - drdtime()); in kvm_set_one_reg()
874 vcpu->arch.st.guest_addr = 0; in kvm_set_one_reg()
875 memset(&vcpu->arch.irq_pending, 0, sizeof(vcpu->arch.irq_pending)); in kvm_set_one_reg()
876 memset(&vcpu->arch.irq_clear, 0, sizeof(vcpu->arch.irq_clear)); in kvm_set_one_reg()
891 static int kvm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) in kvm_set_reg() argument
906 return kvm_set_one_reg(vcpu, reg, v); in kvm_set_reg()
909 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) in kvm_arch_vcpu_ioctl_get_sregs() argument
914 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) in kvm_arch_vcpu_ioctl_set_sregs() argument
919 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) in kvm_arch_vcpu_ioctl_get_regs() argument
923 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++) in kvm_arch_vcpu_ioctl_get_regs()
924 regs->gpr[i] = vcpu->arch.gprs[i]; in kvm_arch_vcpu_ioctl_get_regs()
926 regs->pc = vcpu->arch.pc; in kvm_arch_vcpu_ioctl_get_regs()
931 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) in kvm_arch_vcpu_ioctl_set_regs() argument
935 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++) in kvm_arch_vcpu_ioctl_set_regs()
936 vcpu->arch.gprs[i] = regs->gpr[i]; in kvm_arch_vcpu_ioctl_set_regs()
938 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */ in kvm_arch_vcpu_ioctl_set_regs()
939 vcpu->arch.pc = regs->pc; in kvm_arch_vcpu_ioctl_set_regs()
944 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, in kvm_vcpu_ioctl_enable_cap() argument
951 static int kvm_loongarch_cpucfg_has_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_cpucfg_has_attr() argument
967 static int kvm_loongarch_pvtime_has_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_pvtime_has_attr() argument
970 if (!kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_STEAL_TIME) in kvm_loongarch_pvtime_has_attr()
977 static int kvm_loongarch_vcpu_has_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_vcpu_has_attr() argument
984 ret = kvm_loongarch_cpucfg_has_attr(vcpu, attr); in kvm_loongarch_vcpu_has_attr()
987 ret = kvm_loongarch_pvtime_has_attr(vcpu, attr); in kvm_loongarch_vcpu_has_attr()
996 static int kvm_loongarch_cpucfg_get_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_cpucfg_get_attr() argument
1010 val = vcpu->kvm->arch.pv_features & LOONGARCH_PV_FEAT_MASK; in kvm_loongarch_cpucfg_get_attr()
1021 static int kvm_loongarch_pvtime_get_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_pvtime_get_attr() argument
1027 if (!kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_STEAL_TIME) in kvm_loongarch_pvtime_get_attr()
1031 gpa = vcpu->arch.st.guest_addr; in kvm_loongarch_pvtime_get_attr()
1038 static int kvm_loongarch_vcpu_get_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_vcpu_get_attr() argument
1045 ret = kvm_loongarch_cpucfg_get_attr(vcpu, attr); in kvm_loongarch_vcpu_get_attr()
1048 ret = kvm_loongarch_pvtime_get_attr(vcpu, attr); in kvm_loongarch_vcpu_get_attr()
1057 static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_cpucfg_set_attr() argument
1062 struct kvm *kvm = vcpu->kvm; in kvm_loongarch_cpucfg_set_attr()
1084 static int kvm_loongarch_pvtime_set_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_pvtime_set_attr() argument
1089 struct kvm *kvm = vcpu->kvm; in kvm_loongarch_pvtime_set_attr()
1091 if (!kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_STEAL_TIME) in kvm_loongarch_pvtime_set_attr()
1102 vcpu->arch.st.guest_addr = gpa; in kvm_loongarch_pvtime_set_attr()
1113 vcpu->arch.st.guest_addr = gpa; in kvm_loongarch_pvtime_set_attr()
1114 vcpu->arch.st.last_steal = current->sched_info.run_delay; in kvm_loongarch_pvtime_set_attr()
1115 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); in kvm_loongarch_pvtime_set_attr()
1121 static int kvm_loongarch_vcpu_set_attr(struct kvm_vcpu *vcpu, in kvm_loongarch_vcpu_set_attr() argument
1128 ret = kvm_loongarch_cpucfg_set_attr(vcpu, attr); in kvm_loongarch_vcpu_set_attr()
1131 ret = kvm_loongarch_pvtime_set_attr(vcpu, attr); in kvm_loongarch_vcpu_set_attr()
1146 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_ioctl() local
1152 * should be used. Since CSR registers owns by this vcpu, if switch in kvm_arch_vcpu_ioctl()
1156 * be clear in vcpu->arch.aux_inuse, and vcpu_load will check in kvm_arch_vcpu_ioctl()
1169 r = kvm_set_reg(vcpu, &reg); in kvm_arch_vcpu_ioctl()
1170 vcpu->arch.aux_inuse &= ~KVM_LARCH_HWCSR_USABLE; in kvm_arch_vcpu_ioctl()
1172 r = kvm_get_reg(vcpu, &reg); in kvm_arch_vcpu_ioctl()
1181 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); in kvm_arch_vcpu_ioctl()
1188 r = kvm_loongarch_vcpu_has_attr(vcpu, &attr); in kvm_arch_vcpu_ioctl()
1195 r = kvm_loongarch_vcpu_get_attr(vcpu, &attr); in kvm_arch_vcpu_ioctl()
1202 r = kvm_loongarch_vcpu_set_attr(vcpu, &attr); in kvm_arch_vcpu_ioctl()
1213 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) in kvm_arch_vcpu_ioctl_get_fpu() argument
1217 fpu->fcc = vcpu->arch.fpu.fcc; in kvm_arch_vcpu_ioctl_get_fpu()
1218 fpu->fcsr = vcpu->arch.fpu.fcsr; in kvm_arch_vcpu_ioctl_get_fpu()
1220 memcpy(&fpu->fpr[i], &vcpu->arch.fpu.fpr[i], FPU_REG_WIDTH / 64); in kvm_arch_vcpu_ioctl_get_fpu()
1225 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) in kvm_arch_vcpu_ioctl_set_fpu() argument
1229 vcpu->arch.fpu.fcc = fpu->fcc; in kvm_arch_vcpu_ioctl_set_fpu()
1230 vcpu->arch.fpu.fcsr = fpu->fcsr; in kvm_arch_vcpu_ioctl_set_fpu()
1232 memcpy(&vcpu->arch.fpu.fpr[i], &fpu->fpr[i], FPU_REG_WIDTH / 64); in kvm_arch_vcpu_ioctl_set_fpu()
1238 int kvm_own_lbt(struct kvm_vcpu *vcpu) in kvm_own_lbt() argument
1240 if (!kvm_guest_has_lbt(&vcpu->arch)) in kvm_own_lbt()
1245 _restore_lbt(&vcpu->arch.lbt); in kvm_own_lbt()
1246 vcpu->arch.aux_inuse |= KVM_LARCH_LBT; in kvm_own_lbt()
1252 static void kvm_lose_lbt(struct kvm_vcpu *vcpu) in kvm_lose_lbt() argument
1255 if (vcpu->arch.aux_inuse & KVM_LARCH_LBT) { in kvm_lose_lbt()
1256 _save_lbt(&vcpu->arch.lbt); in kvm_lose_lbt()
1258 vcpu->arch.aux_inuse &= ~KVM_LARCH_LBT; in kvm_lose_lbt()
1263 static void kvm_check_fcsr(struct kvm_vcpu *vcpu, unsigned long fcsr) in kvm_check_fcsr() argument
1270 kvm_own_lbt(vcpu); in kvm_check_fcsr()
1273 static void kvm_check_fcsr_alive(struct kvm_vcpu *vcpu) in kvm_check_fcsr_alive() argument
1275 if (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { in kvm_check_fcsr_alive()
1276 if (vcpu->arch.aux_inuse & KVM_LARCH_LBT) in kvm_check_fcsr_alive()
1278 kvm_check_fcsr(vcpu, read_fcsr(LOONGARCH_FCSR0)); in kvm_check_fcsr_alive()
1282 static inline void kvm_lose_lbt(struct kvm_vcpu *vcpu) { } in kvm_lose_lbt() argument
1283 static inline void kvm_check_fcsr(struct kvm_vcpu *vcpu, unsigned long fcsr) { } in kvm_check_fcsr() argument
1284 static inline void kvm_check_fcsr_alive(struct kvm_vcpu *vcpu) { } in kvm_check_fcsr_alive() argument
1288 void kvm_own_fpu(struct kvm_vcpu *vcpu) in kvm_own_fpu() argument
1296 kvm_check_fcsr(vcpu, vcpu->arch.fpu.fcsr); in kvm_own_fpu()
1299 kvm_restore_fpu(&vcpu->arch.fpu); in kvm_own_fpu()
1300 vcpu->arch.aux_inuse |= KVM_LARCH_FPU; in kvm_own_fpu()
1301 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU); in kvm_own_fpu()
1308 int kvm_own_lsx(struct kvm_vcpu *vcpu) in kvm_own_lsx() argument
1310 if (!kvm_guest_has_fpu(&vcpu->arch) || !kvm_guest_has_lsx(&vcpu->arch)) in kvm_own_lsx()
1316 kvm_check_fcsr(vcpu, vcpu->arch.fpu.fcsr); in kvm_own_lsx()
1318 switch (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { in kvm_own_lsx()
1324 _restore_lsx_upper(&vcpu->arch.fpu); in kvm_own_lsx()
1330 kvm_restore_lsx(&vcpu->arch.fpu); in kvm_own_lsx()
1334 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_LSX); in kvm_own_lsx()
1335 vcpu->arch.aux_inuse |= KVM_LARCH_LSX | KVM_LARCH_FPU; in kvm_own_lsx()
1344 int kvm_own_lasx(struct kvm_vcpu *vcpu) in kvm_own_lasx() argument
1346 …if (!kvm_guest_has_fpu(&vcpu->arch) || !kvm_guest_has_lsx(&vcpu->arch) || !kvm_guest_has_lasx(&vcp… in kvm_own_lasx()
1351 kvm_check_fcsr(vcpu, vcpu->arch.fpu.fcsr); in kvm_own_lasx()
1353 switch (vcpu->arch.aux_inuse & (KVM_LARCH_FPU | KVM_LARCH_LSX)) { in kvm_own_lasx()
1357 _restore_lasx_upper(&vcpu->arch.fpu); in kvm_own_lasx()
1361 _restore_lsx_upper(&vcpu->arch.fpu); in kvm_own_lasx()
1362 _restore_lasx_upper(&vcpu->arch.fpu); in kvm_own_lasx()
1366 kvm_restore_lasx(&vcpu->arch.fpu); in kvm_own_lasx()
1370 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_LASX); in kvm_own_lasx()
1371 vcpu->arch.aux_inuse |= KVM_LARCH_LASX | KVM_LARCH_LSX | KVM_LARCH_FPU; in kvm_own_lasx()
1379 void kvm_lose_fpu(struct kvm_vcpu *vcpu) in kvm_lose_fpu() argument
1383 kvm_check_fcsr_alive(vcpu); in kvm_lose_fpu()
1384 if (vcpu->arch.aux_inuse & KVM_LARCH_LASX) { in kvm_lose_fpu()
1385 kvm_save_lasx(&vcpu->arch.fpu); in kvm_lose_fpu()
1386 vcpu->arch.aux_inuse &= ~(KVM_LARCH_LSX | KVM_LARCH_FPU | KVM_LARCH_LASX); in kvm_lose_fpu()
1387 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_LASX); in kvm_lose_fpu()
1391 } else if (vcpu->arch.aux_inuse & KVM_LARCH_LSX) { in kvm_lose_fpu()
1392 kvm_save_lsx(&vcpu->arch.fpu); in kvm_lose_fpu()
1393 vcpu->arch.aux_inuse &= ~(KVM_LARCH_LSX | KVM_LARCH_FPU); in kvm_lose_fpu()
1394 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_LSX); in kvm_lose_fpu()
1398 } else if (vcpu->arch.aux_inuse & KVM_LARCH_FPU) { in kvm_lose_fpu()
1399 kvm_save_fpu(&vcpu->arch.fpu); in kvm_lose_fpu()
1400 vcpu->arch.aux_inuse &= ~KVM_LARCH_FPU; in kvm_lose_fpu()
1401 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU); in kvm_lose_fpu()
1406 kvm_lose_lbt(vcpu); in kvm_lose_fpu()
1411 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) in kvm_vcpu_ioctl_interrupt() argument
1416 kvm_queue_irq(vcpu, intr); in kvm_vcpu_ioctl_interrupt()
1418 kvm_dequeue_irq(vcpu, -intr); in kvm_vcpu_ioctl_interrupt()
1424 kvm_vcpu_kick(vcpu); in kvm_vcpu_ioctl_interrupt()
1433 struct kvm_vcpu *vcpu = filp->private_data; in kvm_arch_vcpu_async_ioctl() local
1441 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__, irq.irq); in kvm_arch_vcpu_async_ioctl()
1443 return kvm_vcpu_ioctl_interrupt(vcpu, &irq); in kvm_arch_vcpu_async_ioctl()
1454 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_create() argument
1459 vcpu->arch.vpid = 0; in kvm_arch_vcpu_create()
1460 vcpu->arch.flush_gpa = INVALID_GPA; in kvm_arch_vcpu_create()
1462 hrtimer_init(&vcpu->arch.swtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); in kvm_arch_vcpu_create()
1463 vcpu->arch.swtimer.function = kvm_swtimer_wakeup; in kvm_arch_vcpu_create()
1465 vcpu->arch.handle_exit = kvm_handle_exit; in kvm_arch_vcpu_create()
1466 vcpu->arch.guest_eentry = (unsigned long)kvm_loongarch_ops->exc_entry; in kvm_arch_vcpu_create()
1467 vcpu->arch.csr = kzalloc(sizeof(struct loongarch_csrs), GFP_KERNEL); in kvm_arch_vcpu_create()
1468 if (!vcpu->arch.csr) in kvm_arch_vcpu_create()
1475 vcpu->arch.host_ecfg = (read_csr_ecfg() & CSR_ECFG_VS); in kvm_arch_vcpu_create()
1478 vcpu->arch.last_sched_cpu = -1; in kvm_arch_vcpu_create()
1481 spin_lock_init(&vcpu->arch.ipi_state.lock); in kvm_arch_vcpu_create()
1487 kvm_init_timer(vcpu, timer_hz); in kvm_arch_vcpu_create()
1490 csr = vcpu->arch.csr; in kvm_arch_vcpu_create()
1494 kvm_write_sw_gcsr(csr, LOONGARCH_CSR_TMID, vcpu->vcpu_id); in kvm_arch_vcpu_create()
1503 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_postcreate() argument
1507 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_destroy() argument
1512 hrtimer_cancel(&vcpu->arch.swtimer); in kvm_arch_vcpu_destroy()
1513 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); in kvm_arch_vcpu_destroy()
1514 kvm_drop_cpuid(vcpu); in kvm_arch_vcpu_destroy()
1515 kfree(vcpu->arch.csr); in kvm_arch_vcpu_destroy()
1518 * If the vCPU is freed and reused as another vCPU, we don't want the in kvm_arch_vcpu_destroy()
1522 context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); in kvm_arch_vcpu_destroy()
1523 if (context->last_vcpu == vcpu) in kvm_arch_vcpu_destroy()
1528 static int _kvm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) in _kvm_vcpu_load() argument
1532 struct loongarch_csrs *csr = vcpu->arch.csr; in _kvm_vcpu_load()
1538 migrated = (vcpu->arch.last_sched_cpu != cpu); in _kvm_vcpu_load()
1541 * Was this the last vCPU to run on this CPU? in _kvm_vcpu_load()
1542 * If not, any old guest state from this vCPU will have been clobbered. in _kvm_vcpu_load()
1544 context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); in _kvm_vcpu_load()
1545 if (migrated || (context->last_vcpu != vcpu)) in _kvm_vcpu_load()
1546 vcpu->arch.aux_inuse &= ~KVM_LARCH_HWCSR_USABLE; in _kvm_vcpu_load()
1547 context->last_vcpu = vcpu; in _kvm_vcpu_load()
1550 kvm_restore_timer(vcpu); in _kvm_vcpu_load()
1554 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); in _kvm_vcpu_load()
1557 kvm_restore_pmu(vcpu); in _kvm_vcpu_load()
1560 if (vcpu->arch.aux_inuse & KVM_LARCH_HWCSR_USABLE) in _kvm_vcpu_load()
1563 write_csr_gcntc((ulong)vcpu->kvm->arch.time_offset); in _kvm_vcpu_load()
1616 * prevents a SC on the next vCPU from succeeding by matching a LL on in _kvm_vcpu_load()
1617 * the previous vCPU. in _kvm_vcpu_load()
1619 if (vcpu->kvm->created_vcpus > 1) in _kvm_vcpu_load()
1622 vcpu->arch.aux_inuse |= KVM_LARCH_HWCSR_USABLE; in _kvm_vcpu_load()
1627 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) in kvm_arch_vcpu_load() argument
1633 _kvm_vcpu_load(vcpu, cpu); in kvm_arch_vcpu_load()
1637 static int _kvm_vcpu_put(struct kvm_vcpu *vcpu, int cpu) in _kvm_vcpu_put() argument
1639 struct loongarch_csrs *csr = vcpu->arch.csr; in _kvm_vcpu_put()
1641 kvm_lose_fpu(vcpu); in _kvm_vcpu_put()
1649 if (vcpu->arch.aux_inuse & KVM_LARCH_SWCSR_LATEST) in _kvm_vcpu_put()
1700 vcpu->arch.aux_inuse |= KVM_LARCH_SWCSR_LATEST; in _kvm_vcpu_put()
1703 kvm_save_timer(vcpu); in _kvm_vcpu_put()
1710 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_put() argument
1717 vcpu->arch.last_sched_cpu = cpu; in kvm_arch_vcpu_put()
1720 _kvm_vcpu_put(vcpu, cpu); in kvm_arch_vcpu_put()
1724 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) in kvm_arch_vcpu_ioctl_run() argument
1727 struct kvm_run *run = vcpu->run; in kvm_arch_vcpu_ioctl_run()
1729 if (vcpu->mmio_needed) { in kvm_arch_vcpu_ioctl_run()
1730 if (!vcpu->mmio_is_write) in kvm_arch_vcpu_ioctl_run()
1731 kvm_complete_mmio_read(vcpu, run); in kvm_arch_vcpu_ioctl_run()
1732 vcpu->mmio_needed = 0; in kvm_arch_vcpu_ioctl_run()
1737 kvm_complete_iocsr_read(vcpu, run); in kvm_arch_vcpu_ioctl_run()
1740 if (!vcpu->wants_to_run) in kvm_arch_vcpu_ioctl_run()
1746 vcpu_load(vcpu); in kvm_arch_vcpu_ioctl_run()
1747 kvm_sigset_activate(vcpu); in kvm_arch_vcpu_ioctl_run()
1748 r = kvm_pre_enter_guest(vcpu); in kvm_arch_vcpu_ioctl_run()
1754 trace_kvm_enter(vcpu); in kvm_arch_vcpu_ioctl_run()
1755 r = kvm_loongarch_ops->enter_guest(run, vcpu); in kvm_arch_vcpu_ioctl_run()
1757 trace_kvm_out(vcpu); in kvm_arch_vcpu_ioctl_run()
1764 kvm_sigset_deactivate(vcpu); in kvm_arch_vcpu_ioctl_run()
1765 vcpu_put(vcpu); in kvm_arch_vcpu_ioctl_run()