Skip to content

Commit b922307

Browse files
committed
RISC-V: KVM: Save/restore SCOUNTEREN in C source
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 <[email protected]> Reviewed-by: Atish Patra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
1 parent b6114a7 commit b922307

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

arch/riscv/kvm/vcpu.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ static __always_inline void kvm_riscv_vcpu_swap_in_guest_state(struct kvm_vcpu *
698698
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
699699
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
700700

701+
vcpu->arch.host_scounteren = csr_swap(CSR_SCOUNTEREN, csr->scounteren);
701702
vcpu->arch.host_senvcfg = csr_swap(CSR_SENVCFG, csr->senvcfg);
702703
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) &&
703704
(cfg->hstateen0 & SMSTATEEN0_SSTATEEN0))
@@ -711,6 +712,7 @@ static __always_inline void kvm_riscv_vcpu_swap_in_host_state(struct kvm_vcpu *v
711712
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
712713
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
713714

715+
csr->scounteren = csr_swap(CSR_SCOUNTEREN, vcpu->arch.host_scounteren);
714716
csr->senvcfg = csr_swap(CSR_SENVCFG, vcpu->arch.host_senvcfg);
715717
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) &&
716718
(cfg->hstateen0 & SMSTATEEN0_SSTATEEN0))

arch/riscv/kvm/vcpu_switch.S

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,25 @@ SYM_FUNC_START(__kvm_riscv_switch_to)
4343

4444
/* Load Guest CSR values */
4545
REG_L t0, (KVM_ARCH_GUEST_SSTATUS)(a0)
46-
REG_L t1, (KVM_ARCH_GUEST_SCOUNTEREN)(a0)
47-
la t3, .Lkvm_switch_return
48-
REG_L t4, (KVM_ARCH_GUEST_SEPC)(a0)
46+
la t1, .Lkvm_switch_return
47+
REG_L t2, (KVM_ARCH_GUEST_SEPC)(a0)
4948

5049
/* Save Host and Restore Guest SSTATUS */
5150
csrrw t0, CSR_SSTATUS, t0
5251

53-
/* Save Host and Restore Guest SCOUNTEREN */
54-
csrrw t1, CSR_SCOUNTEREN, t1
55-
5652
/* Save Host STVEC and change it to return path */
57-
csrrw t3, CSR_STVEC, t3
58-
59-
/* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */
60-
csrrw t2, CSR_SSCRATCH, a0
53+
csrrw t1, CSR_STVEC, t1
6154

6255
/* Restore Guest SEPC */
63-
csrw CSR_SEPC, t4
56+
csrw CSR_SEPC, t2
57+
58+
/* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */
59+
csrrw t3, CSR_SSCRATCH, a0
6460

6561
/* Store Host CSR values */
6662
REG_S t0, (KVM_ARCH_HOST_SSTATUS)(a0)
67-
REG_S t1, (KVM_ARCH_HOST_SCOUNTEREN)(a0)
68-
REG_S t2, (KVM_ARCH_HOST_SSCRATCH)(a0)
69-
REG_S t3, (KVM_ARCH_HOST_STVEC)(a0)
63+
REG_S t1, (KVM_ARCH_HOST_STVEC)(a0)
64+
REG_S t3, (KVM_ARCH_HOST_SSCRATCH)(a0)
7065

7166
/* Restore Guest GPRs (except A0) */
7267
REG_L ra, (KVM_ARCH_GUEST_RA)(a0)
@@ -145,31 +140,26 @@ SYM_FUNC_START(__kvm_riscv_switch_to)
145140
REG_S t6, (KVM_ARCH_GUEST_T6)(a0)
146141

147142
/* Load Host CSR values */
148-
REG_L t1, (KVM_ARCH_HOST_STVEC)(a0)
149-
REG_L t2, (KVM_ARCH_HOST_SSCRATCH)(a0)
150-
REG_L t3, (KVM_ARCH_HOST_SCOUNTEREN)(a0)
151-
REG_L t4, (KVM_ARCH_HOST_SSTATUS)(a0)
152-
153-
/* Save Guest SEPC */
154-
csrr t0, CSR_SEPC
143+
REG_L t0, (KVM_ARCH_HOST_STVEC)(a0)
144+
REG_L t1, (KVM_ARCH_HOST_SSCRATCH)(a0)
145+
REG_L t2, (KVM_ARCH_HOST_SSTATUS)(a0)
155146

156147
/* Save Guest A0 and Restore Host SSCRATCH */
157-
csrrw t2, CSR_SSCRATCH, t2
148+
csrrw t1, CSR_SSCRATCH, t1
158149

159-
/* Restore Host STVEC */
160-
csrw CSR_STVEC, t1
150+
/* Save Guest SEPC */
151+
csrr t3, CSR_SEPC
161152

162-
/* Save Guest and Restore Host SCOUNTEREN */
163-
csrrw t3, CSR_SCOUNTEREN, t3
153+
/* Restore Host STVEC */
154+
csrw CSR_STVEC, t0
164155

165156
/* Save Guest and Restore Host SSTATUS */
166-
csrrw t4, CSR_SSTATUS, t4
157+
csrrw t2, CSR_SSTATUS, t2
167158

168159
/* Store Guest CSR values */
169-
REG_S t0, (KVM_ARCH_GUEST_SEPC)(a0)
170-
REG_S t2, (KVM_ARCH_GUEST_A0)(a0)
171-
REG_S t3, (KVM_ARCH_GUEST_SCOUNTEREN)(a0)
172-
REG_S t4, (KVM_ARCH_GUEST_SSTATUS)(a0)
160+
REG_S t1, (KVM_ARCH_GUEST_A0)(a0)
161+
REG_S t2, (KVM_ARCH_GUEST_SSTATUS)(a0)
162+
REG_S t3, (KVM_ARCH_GUEST_SEPC)(a0)
173163

174164
/* Restore Host GPRs (except A0 and T0-T6) */
175165
REG_L ra, (KVM_ARCH_HOST_RA)(a0)

0 commit comments

Comments
 (0)