]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
RISC-V: KVM: Save/restore SCOUNTEREN in C source
authorAnup Patel <apatel@ventanamicro.com>
Sun, 20 Oct 2024 19:47:24 +0000 (01:17 +0530)
committerAnup Patel <anup@brainfault.org>
Mon, 28 Oct 2024 11:13:43 +0000 (16:43 +0530)
The SCOUNTEREN CSR need not be saved/restored in the low-level
__kvm_riscv_switch_to() function hence move the SCOUNTEREN CSR
save/restore to the kvm_riscv_vcpu_swap_in_guest_state() and
kvm_riscv_vcpu_swap_in_host_state() functions in C sources.

Also, re-arrange the CSR save/restore and related GPR usage in
the low-level __kvm_riscv_switch_to() low-level function.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20241020194734.58686-4-apatel@ventanamicro.com
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/kvm/vcpu.c
arch/riscv/kvm/vcpu_switch.S

index c9933ea170f40f369a57c13b8cd38bcf80f88a51..05ccb3b97bab77dcfe03099feaa82c52bb3a7387 100644 (file)
@@ -698,6 +698,7 @@ static __always_inline void kvm_riscv_vcpu_swap_in_guest_state(struct kvm_vcpu *
        struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
        struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
 
+       vcpu->arch.host_scounteren = csr_swap(CSR_SCOUNTEREN, csr->scounteren);
        vcpu->arch.host_senvcfg = csr_swap(CSR_SENVCFG, csr->senvcfg);
        if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) &&
            (cfg->hstateen0 & SMSTATEEN0_SSTATEEN0))
@@ -711,6 +712,7 @@ static __always_inline void kvm_riscv_vcpu_swap_in_host_state(struct kvm_vcpu *v
        struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
        struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
 
+       csr->scounteren = csr_swap(CSR_SCOUNTEREN, vcpu->arch.host_scounteren);
        csr->senvcfg = csr_swap(CSR_SENVCFG, vcpu->arch.host_senvcfg);
        if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) &&
            (cfg->hstateen0 & SMSTATEEN0_SSTATEEN0))
index f83643c4fdb9031f10ef18bd11f17f2a80887a4c..3f8cbc21a644f8c2a6a106d2445ac4de4e664c26 100644 (file)
@@ -43,30 +43,25 @@ SYM_FUNC_START(__kvm_riscv_switch_to)
 
        /* Load Guest CSR values */
        REG_L   t0, (KVM_ARCH_GUEST_SSTATUS)(a0)
-       REG_L   t1, (KVM_ARCH_GUEST_SCOUNTEREN)(a0)
-       la      t3, .Lkvm_switch_return
-       REG_L   t4, (KVM_ARCH_GUEST_SEPC)(a0)
+       la      t1, .Lkvm_switch_return
+       REG_L   t2, (KVM_ARCH_GUEST_SEPC)(a0)
 
        /* Save Host and Restore Guest SSTATUS */
        csrrw   t0, CSR_SSTATUS, t0
 
-       /* Save Host and Restore Guest SCOUNTEREN */
-       csrrw   t1, CSR_SCOUNTEREN, t1
-
        /* Save Host STVEC and change it to return path */
-       csrrw   t3, CSR_STVEC, t3
-
-       /* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */
-       csrrw   t2, CSR_SSCRATCH, a0
+       csrrw   t1, CSR_STVEC, t1
 
        /* Restore Guest SEPC */
-       csrw    CSR_SEPC, t4
+       csrw    CSR_SEPC, t2
+
+       /* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */
+       csrrw   t3, CSR_SSCRATCH, a0
 
        /* Store Host CSR values */
        REG_S   t0, (KVM_ARCH_HOST_SSTATUS)(a0)
-       REG_S   t1, (KVM_ARCH_HOST_SCOUNTEREN)(a0)
-       REG_S   t2, (KVM_ARCH_HOST_SSCRATCH)(a0)
-       REG_S   t3, (KVM_ARCH_HOST_STVEC)(a0)
+       REG_S   t1, (KVM_ARCH_HOST_STVEC)(a0)
+       REG_S   t3, (KVM_ARCH_HOST_SSCRATCH)(a0)
 
        /* Restore Guest GPRs (except A0) */
        REG_L   ra, (KVM_ARCH_GUEST_RA)(a0)
@@ -145,31 +140,26 @@ SYM_FUNC_START(__kvm_riscv_switch_to)
        REG_S   t6, (KVM_ARCH_GUEST_T6)(a0)
 
        /* Load Host CSR values */
-       REG_L   t1, (KVM_ARCH_HOST_STVEC)(a0)
-       REG_L   t2, (KVM_ARCH_HOST_SSCRATCH)(a0)
-       REG_L   t3, (KVM_ARCH_HOST_SCOUNTEREN)(a0)
-       REG_L   t4, (KVM_ARCH_HOST_SSTATUS)(a0)
-
-       /* Save Guest SEPC */
-       csrr    t0, CSR_SEPC
+       REG_L   t0, (KVM_ARCH_HOST_STVEC)(a0)
+       REG_L   t1, (KVM_ARCH_HOST_SSCRATCH)(a0)
+       REG_L   t2, (KVM_ARCH_HOST_SSTATUS)(a0)
 
        /* Save Guest A0 and Restore Host SSCRATCH */
-       csrrw   t2, CSR_SSCRATCH, t2
+       csrrw   t1, CSR_SSCRATCH, t1
 
-       /* Restore Host STVEC */
-       csrw    CSR_STVEC, t1
+       /* Save Guest SEPC */
+       csrr    t3, CSR_SEPC
 
-       /* Save Guest and Restore Host SCOUNTEREN */
-       csrrw   t3, CSR_SCOUNTEREN, t3
+       /* Restore Host STVEC */
+       csrw    CSR_STVEC, t0
 
        /* Save Guest and Restore Host SSTATUS */
-       csrrw   t4, CSR_SSTATUS, t4
+       csrrw   t2, CSR_SSTATUS, t2
 
        /* Store Guest CSR values */
-       REG_S   t0, (KVM_ARCH_GUEST_SEPC)(a0)
-       REG_S   t2, (KVM_ARCH_GUEST_A0)(a0)
-       REG_S   t3, (KVM_ARCH_GUEST_SCOUNTEREN)(a0)
-       REG_S   t4, (KVM_ARCH_GUEST_SSTATUS)(a0)
+       REG_S   t1, (KVM_ARCH_GUEST_A0)(a0)
+       REG_S   t2, (KVM_ARCH_GUEST_SSTATUS)(a0)
+       REG_S   t3, (KVM_ARCH_GUEST_SEPC)(a0)
 
        /* Restore Host GPRs (except A0 and T0-T6) */
        REG_L   ra, (KVM_ARCH_HOST_RA)(a0)