Skip to content

Commit b6114a7

Browse files
committed
RISC-V: KVM: Save/restore HSTATUS in C source
We will be optimizing HSTATUS CSR access via shared memory setup using the SBI nested acceleration extension. To facilitate this, we first move HSTATUS save/restore in kvm_riscv_vcpu_enter_exit(). 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 e403a90 commit b6114a7

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

arch/riscv/kvm/vcpu.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,18 @@ static __always_inline void kvm_riscv_vcpu_swap_in_host_state(struct kvm_vcpu *v
727727
*/
728728
static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu)
729729
{
730+
struct kvm_cpu_context *gcntx = &vcpu->arch.guest_context;
731+
struct kvm_cpu_context *hcntx = &vcpu->arch.host_context;
732+
730733
kvm_riscv_vcpu_swap_in_guest_state(vcpu);
731734
guest_state_enter_irqoff();
735+
736+
hcntx->hstatus = csr_swap(CSR_HSTATUS, gcntx->hstatus);
737+
732738
__kvm_riscv_switch_to(&vcpu->arch);
739+
740+
gcntx->hstatus = csr_swap(CSR_HSTATUS, hcntx->hstatus);
741+
733742
vcpu->arch.last_exit_cpu = vcpu->cpu;
734743
guest_state_exit_irqoff();
735744
kvm_riscv_vcpu_swap_in_host_state(vcpu);

arch/riscv/kvm/vcpu_switch.S

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,30 @@ 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_HSTATUS)(a0)
47-
REG_L t2, (KVM_ARCH_GUEST_SCOUNTEREN)(a0)
48-
la t4, .Lkvm_switch_return
49-
REG_L t5, (KVM_ARCH_GUEST_SEPC)(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)
5049

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

54-
/* Save Host and Restore Guest HSTATUS */
55-
csrrw t1, CSR_HSTATUS, t1
56-
5753
/* Save Host and Restore Guest SCOUNTEREN */
58-
csrrw t2, CSR_SCOUNTEREN, t2
54+
csrrw t1, CSR_SCOUNTEREN, t1
5955

6056
/* Save Host STVEC and change it to return path */
61-
csrrw t4, CSR_STVEC, t4
57+
csrrw t3, CSR_STVEC, t3
6258

6359
/* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */
64-
csrrw t3, CSR_SSCRATCH, a0
60+
csrrw t2, CSR_SSCRATCH, a0
6561

6662
/* Restore Guest SEPC */
67-
csrw CSR_SEPC, t5
63+
csrw CSR_SEPC, t4
6864

6965
/* Store Host CSR values */
7066
REG_S t0, (KVM_ARCH_HOST_SSTATUS)(a0)
71-
REG_S t1, (KVM_ARCH_HOST_HSTATUS)(a0)
72-
REG_S t2, (KVM_ARCH_HOST_SCOUNTEREN)(a0)
73-
REG_S t3, (KVM_ARCH_HOST_SSCRATCH)(a0)
74-
REG_S t4, (KVM_ARCH_HOST_STVEC)(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)
7570

7671
/* Restore Guest GPRs (except A0) */
7772
REG_L ra, (KVM_ARCH_GUEST_RA)(a0)
@@ -153,8 +148,7 @@ SYM_FUNC_START(__kvm_riscv_switch_to)
153148
REG_L t1, (KVM_ARCH_HOST_STVEC)(a0)
154149
REG_L t2, (KVM_ARCH_HOST_SSCRATCH)(a0)
155150
REG_L t3, (KVM_ARCH_HOST_SCOUNTEREN)(a0)
156-
REG_L t4, (KVM_ARCH_HOST_HSTATUS)(a0)
157-
REG_L t5, (KVM_ARCH_HOST_SSTATUS)(a0)
151+
REG_L t4, (KVM_ARCH_HOST_SSTATUS)(a0)
158152

159153
/* Save Guest SEPC */
160154
csrr t0, CSR_SEPC
@@ -168,18 +162,14 @@ SYM_FUNC_START(__kvm_riscv_switch_to)
168162
/* Save Guest and Restore Host SCOUNTEREN */
169163
csrrw t3, CSR_SCOUNTEREN, t3
170164

171-
/* Save Guest and Restore Host HSTATUS */
172-
csrrw t4, CSR_HSTATUS, t4
173-
174165
/* Save Guest and Restore Host SSTATUS */
175-
csrrw t5, CSR_SSTATUS, t5
166+
csrrw t4, CSR_SSTATUS, t4
176167

177168
/* Store Guest CSR values */
178169
REG_S t0, (KVM_ARCH_GUEST_SEPC)(a0)
179170
REG_S t2, (KVM_ARCH_GUEST_A0)(a0)
180171
REG_S t3, (KVM_ARCH_GUEST_SCOUNTEREN)(a0)
181-
REG_S t4, (KVM_ARCH_GUEST_HSTATUS)(a0)
182-
REG_S t5, (KVM_ARCH_GUEST_SSTATUS)(a0)
172+
REG_S t4, (KVM_ARCH_GUEST_SSTATUS)(a0)
183173

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

0 commit comments

Comments
 (0)