1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
6 * Anup Patel <anup.patel@wdc.com>
9 #include <linux/bitops.h>
10 #include <linux/entry-kvm.h>
11 #include <linux/errno.h>
12 #include <linux/err.h>
13 #include <linux/kdebug.h>
14 #include <linux/module.h>
15 #include <linux/percpu.h>
16 #include <linux/vmalloc.h>
17 #include <linux/sched/signal.h>
19 #include <linux/kvm_host.h>
21 #include <asm/cacheflush.h>
22 #include <asm/kvm_vcpu_vector.h>
24 const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
25 KVM_GENERIC_VCPU_STATS(),
26 STATS_DESC_COUNTER(VCPU, ecall_exit_stat),
27 STATS_DESC_COUNTER(VCPU, wfi_exit_stat),
28 STATS_DESC_COUNTER(VCPU, mmio_exit_user),
29 STATS_DESC_COUNTER(VCPU, mmio_exit_kernel),
30 STATS_DESC_COUNTER(VCPU, csr_exit_user),
31 STATS_DESC_COUNTER(VCPU, csr_exit_kernel),
32 STATS_DESC_COUNTER(VCPU, signal_exits),
33 STATS_DESC_COUNTER(VCPU, exits)
36 const struct kvm_stats_header kvm_vcpu_stats_header = {
37 .name_size = KVM_STATS_NAME_SIZE,
38 .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
39 .id_offset = sizeof(struct kvm_stats_header),
40 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
41 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
42 sizeof(kvm_vcpu_stats_desc),
45 static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
47 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
48 struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr;
49 struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
50 struct kvm_cpu_context *reset_cntx = &vcpu->arch.guest_reset_context;
54 * The preemption should be disabled here because it races with
55 * kvm_sched_out/kvm_sched_in(called from preempt notifiers) which
56 * also calls vcpu_load/put.
59 loaded = (vcpu->cpu != -1);
61 kvm_arch_vcpu_put(vcpu);
63 vcpu->arch.last_exit_cpu = -1;
65 memcpy(csr, reset_csr, sizeof(*csr));
67 memcpy(cntx, reset_cntx, sizeof(*cntx));
69 kvm_riscv_vcpu_fp_reset(vcpu);
71 kvm_riscv_vcpu_vector_reset(vcpu);
73 kvm_riscv_vcpu_timer_reset(vcpu);
75 kvm_riscv_vcpu_aia_reset(vcpu);
77 bitmap_zero(vcpu->arch.irqs_pending, KVM_RISCV_VCPU_NR_IRQS);
78 bitmap_zero(vcpu->arch.irqs_pending_mask, KVM_RISCV_VCPU_NR_IRQS);
80 kvm_riscv_vcpu_pmu_reset(vcpu);
82 vcpu->arch.hfence_head = 0;
83 vcpu->arch.hfence_tail = 0;
84 memset(vcpu->arch.hfence_queue, 0, sizeof(vcpu->arch.hfence_queue));
86 /* Reset the guest CSRs for hotplug usecase */
88 kvm_arch_vcpu_load(vcpu, smp_processor_id());
92 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
97 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
100 struct kvm_cpu_context *cntx;
101 struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr;
103 /* Mark this VCPU never ran */
104 vcpu->arch.ran_atleast_once = false;
105 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
106 bitmap_zero(vcpu->arch.isa, RISCV_ISA_EXT_MAX);
108 /* Setup ISA features available to VCPU */
109 kvm_riscv_vcpu_setup_isa(vcpu);
111 /* Setup vendor, arch, and implementation details */
112 vcpu->arch.mvendorid = sbi_get_mvendorid();
113 vcpu->arch.marchid = sbi_get_marchid();
114 vcpu->arch.mimpid = sbi_get_mimpid();
116 /* Setup VCPU hfence queue */
117 spin_lock_init(&vcpu->arch.hfence_lock);
119 /* Setup reset state of shadow SSTATUS and HSTATUS CSRs */
120 cntx = &vcpu->arch.guest_reset_context;
121 cntx->sstatus = SR_SPP | SR_SPIE;
123 cntx->hstatus |= HSTATUS_VTW;
124 cntx->hstatus |= HSTATUS_SPVP;
125 cntx->hstatus |= HSTATUS_SPV;
127 if (kvm_riscv_vcpu_alloc_vector_context(vcpu, cntx))
130 /* By default, make CY, TM, and IR counters accessible in VU mode */
131 reset_csr->scounteren = 0x7;
133 /* Setup VCPU timer */
134 kvm_riscv_vcpu_timer_init(vcpu);
136 /* setup performance monitoring */
137 kvm_riscv_vcpu_pmu_init(vcpu);
140 rc = kvm_riscv_vcpu_aia_init(vcpu);
145 * Setup SBI extensions
146 * NOTE: This must be the last thing to be initialized.
148 kvm_riscv_vcpu_sbi_init(vcpu);
151 kvm_riscv_reset_vcpu(vcpu);
156 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
159 * vcpu with id 0 is the designated boot cpu.
160 * Keep all vcpus with non-zero id in power-off state so that
161 * they can be brought up using SBI HSM extension.
163 if (vcpu->vcpu_idx != 0)
164 kvm_riscv_vcpu_power_off(vcpu);
167 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
169 /* Cleanup VCPU AIA context */
170 kvm_riscv_vcpu_aia_deinit(vcpu);
172 /* Cleanup VCPU timer */
173 kvm_riscv_vcpu_timer_deinit(vcpu);
175 kvm_riscv_vcpu_pmu_deinit(vcpu);
177 /* Free unused pages pre-allocated for G-stage page table mappings */
178 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
180 /* Free vector context space for host and guest kernel */
181 kvm_riscv_vcpu_free_vector_context(vcpu);
184 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
186 return kvm_riscv_vcpu_timer_pending(vcpu);
189 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
191 kvm_riscv_aia_wakeon_hgei(vcpu, true);
194 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
196 kvm_riscv_aia_wakeon_hgei(vcpu, false);
199 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
201 return (kvm_riscv_vcpu_has_interrupts(vcpu, -1UL) &&
202 !vcpu->arch.power_off && !vcpu->arch.pause);
205 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
207 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
210 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
212 return (vcpu->arch.guest_context.sstatus & SR_SPP) ? true : false;
215 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
217 return VM_FAULT_SIGBUS;
220 long kvm_arch_vcpu_async_ioctl(struct file *filp,
221 unsigned int ioctl, unsigned long arg)
223 struct kvm_vcpu *vcpu = filp->private_data;
224 void __user *argp = (void __user *)arg;
226 if (ioctl == KVM_INTERRUPT) {
227 struct kvm_interrupt irq;
229 if (copy_from_user(&irq, argp, sizeof(irq)))
232 if (irq.irq == KVM_INTERRUPT_SET)
233 return kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_VS_EXT);
235 return kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_VS_EXT);
241 long kvm_arch_vcpu_ioctl(struct file *filp,
242 unsigned int ioctl, unsigned long arg)
244 struct kvm_vcpu *vcpu = filp->private_data;
245 void __user *argp = (void __user *)arg;
249 case KVM_SET_ONE_REG:
250 case KVM_GET_ONE_REG: {
251 struct kvm_one_reg reg;
254 if (copy_from_user(®, argp, sizeof(reg)))
257 if (ioctl == KVM_SET_ONE_REG)
258 r = kvm_riscv_vcpu_set_reg(vcpu, ®);
260 r = kvm_riscv_vcpu_get_reg(vcpu, ®);
263 case KVM_GET_REG_LIST: {
264 struct kvm_reg_list __user *user_list = argp;
265 struct kvm_reg_list reg_list;
269 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
272 reg_list.n = kvm_riscv_vcpu_num_regs(vcpu);
273 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
278 r = kvm_riscv_vcpu_copy_reg_indices(vcpu, user_list->reg);
288 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
289 struct kvm_sregs *sregs)
294 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
295 struct kvm_sregs *sregs)
300 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
305 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
310 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
311 struct kvm_translation *tr)
316 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
321 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
326 void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu)
328 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
329 unsigned long mask, val;
331 if (READ_ONCE(vcpu->arch.irqs_pending_mask[0])) {
332 mask = xchg_acquire(&vcpu->arch.irqs_pending_mask[0], 0);
333 val = READ_ONCE(vcpu->arch.irqs_pending[0]) & mask;
339 /* Flush AIA high interrupts */
340 kvm_riscv_vcpu_aia_flush_interrupts(vcpu);
343 void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu)
346 struct kvm_vcpu_arch *v = &vcpu->arch;
347 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
349 /* Read current HVIP and VSIE CSRs */
350 csr->vsie = csr_read(CSR_VSIE);
352 /* Sync-up HVIP.VSSIP bit changes does by Guest */
353 hvip = csr_read(CSR_HVIP);
354 if ((csr->hvip ^ hvip) & (1UL << IRQ_VS_SOFT)) {
355 if (hvip & (1UL << IRQ_VS_SOFT)) {
356 if (!test_and_set_bit(IRQ_VS_SOFT,
357 v->irqs_pending_mask))
358 set_bit(IRQ_VS_SOFT, v->irqs_pending);
360 if (!test_and_set_bit(IRQ_VS_SOFT,
361 v->irqs_pending_mask))
362 clear_bit(IRQ_VS_SOFT, v->irqs_pending);
366 /* Sync-up AIA high interrupts */
367 kvm_riscv_vcpu_aia_sync_interrupts(vcpu);
369 /* Sync-up timer CSRs */
370 kvm_riscv_vcpu_timer_sync(vcpu);
373 int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)
376 * We only allow VS-mode software, timer, and external
377 * interrupts when irq is one of the local interrupts
378 * defined by RISC-V privilege specification.
380 if (irq < IRQ_LOCAL_MAX &&
381 irq != IRQ_VS_SOFT &&
382 irq != IRQ_VS_TIMER &&
386 set_bit(irq, vcpu->arch.irqs_pending);
387 smp_mb__before_atomic();
388 set_bit(irq, vcpu->arch.irqs_pending_mask);
395 int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)
398 * We only allow VS-mode software, timer, and external
399 * interrupts when irq is one of the local interrupts
400 * defined by RISC-V privilege specification.
402 if (irq < IRQ_LOCAL_MAX &&
403 irq != IRQ_VS_SOFT &&
404 irq != IRQ_VS_TIMER &&
408 clear_bit(irq, vcpu->arch.irqs_pending);
409 smp_mb__before_atomic();
410 set_bit(irq, vcpu->arch.irqs_pending_mask);
415 bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask)
419 ie = ((vcpu->arch.guest_csr.vsie & VSIP_VALID_MASK)
420 << VSIP_TO_HVIP_SHIFT) & (unsigned long)mask;
421 ie |= vcpu->arch.guest_csr.vsie & ~IRQ_LOCAL_MASK &
423 if (READ_ONCE(vcpu->arch.irqs_pending[0]) & ie)
426 /* Check AIA high interrupts */
427 return kvm_riscv_vcpu_aia_has_interrupts(vcpu, mask);
430 void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu)
432 vcpu->arch.power_off = true;
433 kvm_make_request(KVM_REQ_SLEEP, vcpu);
437 void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu)
439 vcpu->arch.power_off = false;
440 kvm_vcpu_wake_up(vcpu);
443 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
444 struct kvm_mp_state *mp_state)
446 if (vcpu->arch.power_off)
447 mp_state->mp_state = KVM_MP_STATE_STOPPED;
449 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
454 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
455 struct kvm_mp_state *mp_state)
459 switch (mp_state->mp_state) {
460 case KVM_MP_STATE_RUNNABLE:
461 vcpu->arch.power_off = false;
463 case KVM_MP_STATE_STOPPED:
464 kvm_riscv_vcpu_power_off(vcpu);
473 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
474 struct kvm_guest_debug *dbg)
476 /* TODO; To be implemented later. */
480 static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)
482 const unsigned long *isa = vcpu->arch.isa;
483 struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
485 if (riscv_isa_extension_available(isa, SVPBMT))
486 cfg->henvcfg |= ENVCFG_PBMTE;
488 if (riscv_isa_extension_available(isa, SSTC))
489 cfg->henvcfg |= ENVCFG_STCE;
491 if (riscv_isa_extension_available(isa, ZICBOM))
492 cfg->henvcfg |= (ENVCFG_CBIE | ENVCFG_CBCFE);
494 if (riscv_isa_extension_available(isa, ZICBOZ))
495 cfg->henvcfg |= ENVCFG_CBZE;
497 if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) {
498 cfg->hstateen0 |= SMSTATEEN0_HSENVCFG;
499 if (riscv_isa_extension_available(isa, SSAIA))
500 cfg->hstateen0 |= SMSTATEEN0_AIA_IMSIC |
503 if (riscv_isa_extension_available(isa, SMSTATEEN))
504 cfg->hstateen0 |= SMSTATEEN0_SSTATEEN0;
508 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
510 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
511 struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
513 csr_write(CSR_VSSTATUS, csr->vsstatus);
514 csr_write(CSR_VSIE, csr->vsie);
515 csr_write(CSR_VSTVEC, csr->vstvec);
516 csr_write(CSR_VSSCRATCH, csr->vsscratch);
517 csr_write(CSR_VSEPC, csr->vsepc);
518 csr_write(CSR_VSCAUSE, csr->vscause);
519 csr_write(CSR_VSTVAL, csr->vstval);
520 csr_write(CSR_HVIP, csr->hvip);
521 csr_write(CSR_VSATP, csr->vsatp);
522 csr_write(CSR_HENVCFG, cfg->henvcfg);
523 if (IS_ENABLED(CONFIG_32BIT))
524 csr_write(CSR_HENVCFGH, cfg->henvcfg >> 32);
525 if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) {
526 csr_write(CSR_HSTATEEN0, cfg->hstateen0);
527 if (IS_ENABLED(CONFIG_32BIT))
528 csr_write(CSR_HSTATEEN0H, cfg->hstateen0 >> 32);
531 kvm_riscv_gstage_update_hgatp(vcpu);
533 kvm_riscv_vcpu_timer_restore(vcpu);
535 kvm_riscv_vcpu_host_fp_save(&vcpu->arch.host_context);
536 kvm_riscv_vcpu_guest_fp_restore(&vcpu->arch.guest_context,
538 kvm_riscv_vcpu_host_vector_save(&vcpu->arch.host_context);
539 kvm_riscv_vcpu_guest_vector_restore(&vcpu->arch.guest_context,
542 kvm_riscv_vcpu_aia_load(vcpu, cpu);
547 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
549 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
553 kvm_riscv_vcpu_aia_put(vcpu);
555 kvm_riscv_vcpu_guest_fp_save(&vcpu->arch.guest_context,
557 kvm_riscv_vcpu_host_fp_restore(&vcpu->arch.host_context);
559 kvm_riscv_vcpu_timer_save(vcpu);
560 kvm_riscv_vcpu_guest_vector_save(&vcpu->arch.guest_context,
562 kvm_riscv_vcpu_host_vector_restore(&vcpu->arch.host_context);
564 csr->vsstatus = csr_read(CSR_VSSTATUS);
565 csr->vsie = csr_read(CSR_VSIE);
566 csr->vstvec = csr_read(CSR_VSTVEC);
567 csr->vsscratch = csr_read(CSR_VSSCRATCH);
568 csr->vsepc = csr_read(CSR_VSEPC);
569 csr->vscause = csr_read(CSR_VSCAUSE);
570 csr->vstval = csr_read(CSR_VSTVAL);
571 csr->hvip = csr_read(CSR_HVIP);
572 csr->vsatp = csr_read(CSR_VSATP);
575 static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)
577 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
579 if (kvm_request_pending(vcpu)) {
580 if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) {
581 kvm_vcpu_srcu_read_unlock(vcpu);
582 rcuwait_wait_event(wait,
583 (!vcpu->arch.power_off) && (!vcpu->arch.pause),
585 kvm_vcpu_srcu_read_lock(vcpu);
587 if (vcpu->arch.power_off || vcpu->arch.pause) {
589 * Awaken to handle a signal, request to
592 kvm_make_request(KVM_REQ_SLEEP, vcpu);
596 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
597 kvm_riscv_reset_vcpu(vcpu);
599 if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu))
600 kvm_riscv_gstage_update_hgatp(vcpu);
602 if (kvm_check_request(KVM_REQ_FENCE_I, vcpu))
603 kvm_riscv_fence_i_process(vcpu);
606 * The generic KVM_REQ_TLB_FLUSH is same as
607 * KVM_REQ_HFENCE_GVMA_VMID_ALL
609 if (kvm_check_request(KVM_REQ_HFENCE_GVMA_VMID_ALL, vcpu))
610 kvm_riscv_hfence_gvma_vmid_all_process(vcpu);
612 if (kvm_check_request(KVM_REQ_HFENCE_VVMA_ALL, vcpu))
613 kvm_riscv_hfence_vvma_all_process(vcpu);
615 if (kvm_check_request(KVM_REQ_HFENCE, vcpu))
616 kvm_riscv_hfence_process(vcpu);
620 static void kvm_riscv_update_hvip(struct kvm_vcpu *vcpu)
622 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
624 csr_write(CSR_HVIP, csr->hvip);
625 kvm_riscv_vcpu_aia_update_hvip(vcpu);
628 static __always_inline void kvm_riscv_vcpu_swap_in_guest_state(struct kvm_vcpu *vcpu)
630 struct kvm_vcpu_smstateen_csr *smcsr = &vcpu->arch.smstateen_csr;
631 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
632 struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
634 vcpu->arch.host_senvcfg = csr_swap(CSR_SENVCFG, csr->senvcfg);
635 if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) &&
636 (cfg->hstateen0 & SMSTATEEN0_SSTATEEN0))
637 vcpu->arch.host_sstateen0 = csr_swap(CSR_SSTATEEN0,
641 static __always_inline void kvm_riscv_vcpu_swap_in_host_state(struct kvm_vcpu *vcpu)
643 struct kvm_vcpu_smstateen_csr *smcsr = &vcpu->arch.smstateen_csr;
644 struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
645 struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
647 csr->senvcfg = csr_swap(CSR_SENVCFG, vcpu->arch.host_senvcfg);
648 if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) &&
649 (cfg->hstateen0 & SMSTATEEN0_SSTATEEN0))
650 smcsr->sstateen0 = csr_swap(CSR_SSTATEEN0,
651 vcpu->arch.host_sstateen0);
655 * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
656 * the vCPU is running.
658 * This must be noinstr as instrumentation may make use of RCU, and this is not
659 * safe during the EQS.
661 static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu)
663 kvm_riscv_vcpu_swap_in_guest_state(vcpu);
664 guest_state_enter_irqoff();
665 __kvm_riscv_switch_to(&vcpu->arch);
666 vcpu->arch.last_exit_cpu = vcpu->cpu;
667 guest_state_exit_irqoff();
668 kvm_riscv_vcpu_swap_in_host_state(vcpu);
671 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
674 struct kvm_cpu_trap trap;
675 struct kvm_run *run = vcpu->run;
677 if (!vcpu->arch.ran_atleast_once)
678 kvm_riscv_vcpu_setup_config(vcpu);
680 /* Mark this VCPU ran at least once */
681 vcpu->arch.ran_atleast_once = true;
683 kvm_vcpu_srcu_read_lock(vcpu);
685 switch (run->exit_reason) {
687 /* Process MMIO value returned from user-space */
688 ret = kvm_riscv_vcpu_mmio_return(vcpu, vcpu->run);
690 case KVM_EXIT_RISCV_SBI:
691 /* Process SBI value returned from user-space */
692 ret = kvm_riscv_vcpu_sbi_return(vcpu, vcpu->run);
694 case KVM_EXIT_RISCV_CSR:
695 /* Process CSR value returned from user-space */
696 ret = kvm_riscv_vcpu_csr_return(vcpu, vcpu->run);
703 kvm_vcpu_srcu_read_unlock(vcpu);
707 if (run->immediate_exit) {
708 kvm_vcpu_srcu_read_unlock(vcpu);
714 kvm_sigset_activate(vcpu);
717 run->exit_reason = KVM_EXIT_UNKNOWN;
719 /* Check conditions before entering the guest */
720 ret = xfer_to_guest_mode_handle_work(vcpu);
725 kvm_riscv_gstage_vmid_update(vcpu);
727 kvm_riscv_check_vcpu_requests(vcpu);
731 /* Update AIA HW state before entering guest */
732 ret = kvm_riscv_vcpu_aia_update(vcpu);
741 * Ensure we set mode to IN_GUEST_MODE after we disable
742 * interrupts and before the final VCPU requests check.
743 * See the comment in kvm_vcpu_exiting_guest_mode() and
744 * Documentation/virt/kvm/vcpu-requests.rst
746 vcpu->mode = IN_GUEST_MODE;
748 kvm_vcpu_srcu_read_unlock(vcpu);
749 smp_mb__after_srcu_read_unlock();
752 * We might have got VCPU interrupts updated asynchronously
753 * so update it in HW.
755 kvm_riscv_vcpu_flush_interrupts(vcpu);
757 /* Update HVIP CSR for current CPU */
758 kvm_riscv_update_hvip(vcpu);
761 kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) ||
762 kvm_request_pending(vcpu) ||
763 xfer_to_guest_mode_work_pending()) {
764 vcpu->mode = OUTSIDE_GUEST_MODE;
767 kvm_vcpu_srcu_read_lock(vcpu);
772 * Cleanup stale TLB enteries
774 * Note: This should be done after G-stage VMID has been
775 * updated using kvm_riscv_gstage_vmid_ver_changed()
777 kvm_riscv_local_tlb_sanitize(vcpu);
779 guest_timing_enter_irqoff();
781 kvm_riscv_vcpu_enter_exit(vcpu);
783 vcpu->mode = OUTSIDE_GUEST_MODE;
787 * Save SCAUSE, STVAL, HTVAL, and HTINST because we might
788 * get an interrupt between __kvm_riscv_switch_to() and
789 * local_irq_enable() which can potentially change CSRs.
791 trap.sepc = vcpu->arch.guest_context.sepc;
792 trap.scause = csr_read(CSR_SCAUSE);
793 trap.stval = csr_read(CSR_STVAL);
794 trap.htval = csr_read(CSR_HTVAL);
795 trap.htinst = csr_read(CSR_HTINST);
797 /* Syncup interrupts state with HW */
798 kvm_riscv_vcpu_sync_interrupts(vcpu);
801 * We must ensure that any pending interrupts are taken before
802 * we exit guest timing so that timer ticks are accounted as
803 * guest time. Transiently unmask interrupts so that any
804 * pending interrupts are taken.
806 * There's no barrier which ensures that pending interrupts are
807 * recognised, so we just hope that the CPU takes any pending
808 * interrupts between the enable and disable.
813 guest_timing_exit_irqoff();
819 kvm_vcpu_srcu_read_lock(vcpu);
821 ret = kvm_riscv_vcpu_exit(vcpu, run, &trap);
824 kvm_sigset_deactivate(vcpu);
828 kvm_vcpu_srcu_read_unlock(vcpu);