Skip to content

Commit 3e7d154

Browse files
committed
RISC-V: KVM: Save trap CSRs in kvm_riscv_vcpu_enter_exit()
Save trap CSRs in the kvm_riscv_vcpu_enter_exit() function instead of the kvm_arch_vcpu_ioctl_run() function so that HTVAL and HTINST CSRs are accessed in more optimized manner while running under some other hypervisor. Signed-off-by: Anup Patel <apatel@ventanamicro.com> Reviewed-by: Atish Patra <atishp@rivosinc.com> Link: https://lore.kernel.org/r/20241020194734.58686-13-apatel@ventanamicro.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent 68c72a6 commit 3e7d154

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

arch/riscv/kvm/vcpu.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,21 @@ static __always_inline void kvm_riscv_vcpu_swap_in_host_state(struct kvm_vcpu *v
764764
* This must be noinstr as instrumentation may make use of RCU, and this is not
765765
* safe during the EQS.
766766
*/
767-
static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu)
767+
static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu,
768+
struct kvm_cpu_trap *trap)
768769
{
769770
void *nsh;
770771
struct kvm_cpu_context *gcntx = &vcpu->arch.guest_context;
771772
struct kvm_cpu_context *hcntx = &vcpu->arch.host_context;
772773

774+
/*
775+
* We save trap CSRs (such as SEPC, SCAUSE, STVAL, HTVAL, and
776+
* HTINST) here because we do local_irq_enable() after this
777+
* function in kvm_arch_vcpu_ioctl_run() which can result in
778+
* an interrupt immediately after local_irq_enable() and can
779+
* potentially change trap CSRs.
780+
*/
781+
773782
kvm_riscv_vcpu_swap_in_guest_state(vcpu);
774783
guest_state_enter_irqoff();
775784

@@ -812,14 +821,24 @@ static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu)
812821
} else {
813822
gcntx->hstatus = csr_swap(CSR_HSTATUS, hcntx->hstatus);
814823
}
824+
825+
trap->htval = nacl_csr_read(nsh, CSR_HTVAL);
826+
trap->htinst = nacl_csr_read(nsh, CSR_HTINST);
815827
} else {
816828
hcntx->hstatus = csr_swap(CSR_HSTATUS, gcntx->hstatus);
817829

818830
__kvm_riscv_switch_to(&vcpu->arch);
819831

820832
gcntx->hstatus = csr_swap(CSR_HSTATUS, hcntx->hstatus);
833+
834+
trap->htval = csr_read(CSR_HTVAL);
835+
trap->htinst = csr_read(CSR_HTINST);
821836
}
822837

838+
trap->sepc = gcntx->sepc;
839+
trap->scause = csr_read(CSR_SCAUSE);
840+
trap->stval = csr_read(CSR_STVAL);
841+
823842
vcpu->arch.last_exit_cpu = vcpu->cpu;
824843
guest_state_exit_irqoff();
825844
kvm_riscv_vcpu_swap_in_host_state(vcpu);
@@ -936,22 +955,11 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
936955

937956
guest_timing_enter_irqoff();
938957

939-
kvm_riscv_vcpu_enter_exit(vcpu);
958+
kvm_riscv_vcpu_enter_exit(vcpu, &trap);
940959

941960
vcpu->mode = OUTSIDE_GUEST_MODE;
942961
vcpu->stat.exits++;
943962

944-
/*
945-
* Save SCAUSE, STVAL, HTVAL, and HTINST because we might
946-
* get an interrupt between __kvm_riscv_switch_to() and
947-
* local_irq_enable() which can potentially change CSRs.
948-
*/
949-
trap.sepc = vcpu->arch.guest_context.sepc;
950-
trap.scause = csr_read(CSR_SCAUSE);
951-
trap.stval = csr_read(CSR_STVAL);
952-
trap.htval = ncsr_read(CSR_HTVAL);
953-
trap.htinst = ncsr_read(CSR_HTINST);
954-
955963
/* Syncup interrupts state with HW */
956964
kvm_riscv_vcpu_sync_interrupts(vcpu);
957965

0 commit comments

Comments
 (0)