Skip to content

Commit 3c39f25

Browse files
atishp04gregkh
authored andcommitted
RISC-V: KVM: Allow legacy PMU access from guest
[ Upstream commit 7d1ffc8 ] Currently, KVM traps & emulates PMU counter access only if SBI PMU is available as the guest can only configure/read PMU counters via SBI only. However, if SBI PMU is not enabled in the host, the guest will fallback to the legacy PMU which will try to access cycle/instret and result in an illegal instruction trap which is not desired. KVM can allow dummy emulation of cycle/instret only for the guest if SBI PMU is not enabled in the host. The dummy emulation will still return zero as we don't to expose the host counter values from a guest using legacy PMU. Fixes: a9ac6c3 ("RISC-V: KVM: Implement trap & emulate for hpmcounters") Signed-off-by: Atish Patra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent a72a99d commit 3c39f25

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

arch/riscv/include/asm/kvm_vcpu_pmu.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define __KVM_VCPU_RISCV_PMU_H
1111

1212
#include <linux/perf/riscv_pmu.h>
13+
#include <asm/kvm_vcpu_insn.h>
1314
#include <asm/sbi.h>
1415

1516
#ifdef CONFIG_RISCV_PMU_SBI
@@ -92,8 +93,20 @@ void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu);
9293
struct kvm_pmu {
9394
};
9495

96+
static inline int kvm_riscv_vcpu_pmu_read_legacy(struct kvm_vcpu *vcpu, unsigned int csr_num,
97+
unsigned long *val, unsigned long new_val,
98+
unsigned long wr_mask)
99+
{
100+
if (csr_num == CSR_CYCLE || csr_num == CSR_INSTRET) {
101+
*val = 0;
102+
return KVM_INSN_CONTINUE_NEXT_SEPC;
103+
} else {
104+
return KVM_INSN_ILLEGAL_TRAP;
105+
}
106+
}
107+
95108
#define KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS \
96-
{.base = 0, .count = 0, .func = NULL },
109+
{.base = CSR_CYCLE, .count = 3, .func = kvm_riscv_vcpu_pmu_read_legacy },
97110

98111
static inline void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) {}
99112
static inline int kvm_riscv_vcpu_pmu_incr_fw(struct kvm_vcpu *vcpu, unsigned long fid)

0 commit comments

Comments
 (0)