Skip to content

Commit 2f15b5e

Browse files
atishp04avpatel
authored andcommitted
RISC-V: KVM: Update firmware counters for various events
SBI PMU specification defines few firmware counters which can be used by the guests to collect the statstics about various traps occurred in the host. Update these counters whenever a corresponding trap is taken Reviewed-by: Anup Patel <anup@brainfault.org> Signed-off-by: Atish Patra <atishp@rivosinc.com> Link: https://lore.kernel.org/r/20241224-kvm_guest_stat-v2-2-08a77ac36b02@rivosinc.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent 51c5895 commit 2f15b5e

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

arch/riscv/kvm/vcpu_exit.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
165165
vcpu->arch.guest_context.sstatus |= SR_SPP;
166166
}
167167

168+
static inline int vcpu_redirect(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap)
169+
{
170+
int ret = -EFAULT;
171+
172+
if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
173+
kvm_riscv_vcpu_trap_redirect(vcpu, trap);
174+
ret = 1;
175+
}
176+
return ret;
177+
}
178+
168179
/*
169180
* Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
170181
* proper exit to userspace.
@@ -183,15 +194,27 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
183194
run->exit_reason = KVM_EXIT_UNKNOWN;
184195
switch (trap->scause) {
185196
case EXC_INST_ILLEGAL:
197+
kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN);
198+
ret = vcpu_redirect(vcpu, trap);
199+
break;
186200
case EXC_LOAD_MISALIGNED:
201+
kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD);
202+
ret = vcpu_redirect(vcpu, trap);
203+
break;
187204
case EXC_STORE_MISALIGNED:
205+
kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_STORE);
206+
ret = vcpu_redirect(vcpu, trap);
207+
break;
188208
case EXC_LOAD_ACCESS:
209+
kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_LOAD);
210+
ret = vcpu_redirect(vcpu, trap);
211+
break;
189212
case EXC_STORE_ACCESS:
213+
kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_STORE);
214+
ret = vcpu_redirect(vcpu, trap);
215+
break;
190216
case EXC_INST_ACCESS:
191-
if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
192-
kvm_riscv_vcpu_trap_redirect(vcpu, trap);
193-
ret = 1;
194-
}
217+
ret = vcpu_redirect(vcpu, trap);
195218
break;
196219
case EXC_VIRTUAL_INST_FAULT:
197220
if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV)

0 commit comments

Comments
 (0)