Skip to content

Commit fe0bab7

Browse files
mdchitaleavpatel
authored andcommitted
RISC-V: KVM: Add kvm_vcpu_config
Add a placeholder for all registers such as henvcfg, hstateen etc which have 'static' configurations depending on extensions supported by the guest. The values are derived once and are then subsequently written to the corresponding CSRs while switching to the vcpu. Signed-off-by: Mayuresh Chitale <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Anup Patel <[email protected]>
1 parent 00c6f39 commit fe0bab7

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

arch/riscv/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ struct kvm_vcpu_csr {
164164
unsigned long scounteren;
165165
};
166166

167+
struct kvm_vcpu_config {
168+
u64 henvcfg;
169+
};
170+
167171
struct kvm_vcpu_arch {
168172
/* VCPU ran at least once */
169173
bool ran_atleast_once;
@@ -244,6 +248,9 @@ struct kvm_vcpu_arch {
244248

245249
/* Performance monitoring context */
246250
struct kvm_pmu pmu_context;
251+
252+
/* 'static' configurations which are set only once */
253+
struct kvm_vcpu_config cfg;
247254
};
248255

249256
static inline void kvm_arch_sync_events(struct kvm *kvm) {}

arch/riscv/kvm/vcpu.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -471,31 +471,28 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
471471
return -EINVAL;
472472
}
473473

474-
static void kvm_riscv_vcpu_update_config(const unsigned long *isa)
474+
static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)
475475
{
476-
u64 henvcfg = 0;
476+
const unsigned long *isa = vcpu->arch.isa;
477+
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
477478

478479
if (riscv_isa_extension_available(isa, SVPBMT))
479-
henvcfg |= ENVCFG_PBMTE;
480+
cfg->henvcfg |= ENVCFG_PBMTE;
480481

481482
if (riscv_isa_extension_available(isa, SSTC))
482-
henvcfg |= ENVCFG_STCE;
483+
cfg->henvcfg |= ENVCFG_STCE;
483484

484485
if (riscv_isa_extension_available(isa, ZICBOM))
485-
henvcfg |= (ENVCFG_CBIE | ENVCFG_CBCFE);
486+
cfg->henvcfg |= (ENVCFG_CBIE | ENVCFG_CBCFE);
486487

487488
if (riscv_isa_extension_available(isa, ZICBOZ))
488-
henvcfg |= ENVCFG_CBZE;
489-
490-
csr_write(CSR_HENVCFG, henvcfg);
491-
#ifdef CONFIG_32BIT
492-
csr_write(CSR_HENVCFGH, henvcfg >> 32);
493-
#endif
489+
cfg->henvcfg |= ENVCFG_CBZE;
494490
}
495491

496492
void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
497493
{
498494
struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr;
495+
struct kvm_vcpu_config *cfg = &vcpu->arch.cfg;
499496

500497
csr_write(CSR_VSSTATUS, csr->vsstatus);
501498
csr_write(CSR_VSIE, csr->vsie);
@@ -506,8 +503,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
506503
csr_write(CSR_VSTVAL, csr->vstval);
507504
csr_write(CSR_HVIP, csr->hvip);
508505
csr_write(CSR_VSATP, csr->vsatp);
509-
510-
kvm_riscv_vcpu_update_config(vcpu->arch.isa);
506+
csr_write(CSR_HENVCFG, cfg->henvcfg);
507+
if (IS_ENABLED(CONFIG_32BIT))
508+
csr_write(CSR_HENVCFGH, cfg->henvcfg >> 32);
511509

512510
kvm_riscv_gstage_update_hgatp(vcpu);
513511

@@ -627,6 +625,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
627625
struct kvm_cpu_trap trap;
628626
struct kvm_run *run = vcpu->run;
629627

628+
if (!vcpu->arch.ran_atleast_once)
629+
kvm_riscv_vcpu_setup_config(vcpu);
630+
630631
/* Mark this VCPU ran at least once */
631632
vcpu->arch.ran_atleast_once = true;
632633

0 commit comments

Comments
 (0)