Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions arch/riscv/include/asm/kvm_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ struct kvm_vcpu_arch {
/* 'static' configurations which are set only once */
struct kvm_vcpu_config cfg;

/* Indicates modified guest CSRs */
bool csr_dirty;

/* SBI steal-time accounting */
struct {
gpa_t shmem;
Expand Down
13 changes: 13 additions & 0 deletions arch/riscv/kvm/vcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define CREATE_TRACE_POINTS
#include "trace.h"

static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu);

const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
KVM_GENERIC_VCPU_STATS(),
STATS_DESC_COUNTER(VCPU, ecall_exit_stat),
Expand Down Expand Up @@ -537,6 +539,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT);
}

/* Mark CSRs dirty on hedeleg update */
vcpu->arch.csr_dirty = true;

return 0;
}

Expand Down Expand Up @@ -581,6 +586,11 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;

if (vcpu->scheduled_out && vcpu == __this_cpu_read(kvm_former_vcpu) &&
vcpu->arch.last_exit_cpu == cpu && !vcpu->arch.csr_dirty)
goto csr_restore_done;

vcpu->arch.csr_dirty = false;
if (kvm_riscv_nacl_sync_csr_available()) {
nsh = nacl_shmem();
nacl_csr_write(nsh, CSR_VSSTATUS, csr->vsstatus);
Expand Down Expand Up @@ -624,6 +634,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)

kvm_riscv_mmu_update_hgatp(vcpu);

csr_restore_done:
kvm_riscv_vcpu_timer_restore(vcpu);

kvm_riscv_vcpu_host_fp_save(&vcpu->arch.host_context);
Expand All @@ -645,6 +656,8 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
void *nsh;
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;

__this_cpu_write(kvm_former_vcpu, vcpu);

vcpu->cpu = -1;

kvm_riscv_vcpu_aia_put(vcpu);
Expand Down
3 changes: 3 additions & 0 deletions arch/riscv/kvm/vcpu_onereg.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu,
if (rc)
return rc;

/* Mark CSRs dirty after userspace update csr */
vcpu->arch.csr_dirty = true;

return 0;
}

Expand Down
Loading