Skip to content

Commit 7d1ffc8

Browse files
atishp04avpatel
authored andcommitted
RISC-V: KVM: Allow legacy PMU access from guest
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]>
1 parent 47d40d9 commit 7d1ffc8

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
@@ -104,8 +105,20 @@ void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu);
104105
struct kvm_pmu {
105106
};
106107

108+
static inline int kvm_riscv_vcpu_pmu_read_legacy(struct kvm_vcpu *vcpu, unsigned int csr_num,
109+
unsigned long *val, unsigned long new_val,
110+
unsigned long wr_mask)
111+
{
112+
if (csr_num == CSR_CYCLE || csr_num == CSR_INSTRET) {
113+
*val = 0;
114+
return KVM_INSN_CONTINUE_NEXT_SEPC;
115+
} else {
116+
return KVM_INSN_ILLEGAL_TRAP;
117+
}
118+
}
119+
107120
#define KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS \
108-
{.base = 0, .count = 0, .func = NULL },
121+
{.base = CSR_CYCLE, .count = 3, .func = kvm_riscv_vcpu_pmu_read_legacy },
109122

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

0 commit comments

Comments
 (0)