Three architecture specified functions are added for guest perf feature, they are kvm_arch_pmi_in_guest(), kvm_arch_vcpu_in_kernel() and kvm_arch_vcpu_get_ip(). Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx> --- arch/loongarch/include/asm/kvm_host.h | 2 ++ arch/loongarch/kvm/vcpu.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include/asm/kvm_host.h index 590982cd986e..49b71ea9c44f 100644 --- a/arch/loongarch/include/asm/kvm_host.h +++ b/arch/loongarch/include/asm/kvm_host.h @@ -12,6 +12,7 @@ #include <linux/kvm.h> #include <linux/kvm_types.h> #include <linux/mutex.h> +#include <linux/perf_event.h> #include <linux/spinlock.h> #include <linux/threads.h> #include <linux/types.h> @@ -291,6 +292,7 @@ static inline int kvm_get_pmu_num(struct kvm_vcpu_arch *arch) /* Debug: dump vcpu state */ int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu); +bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu); /* MMU handling */ void kvm_flush_tlb_all(void); diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 3318a55a0699..fb85c38f4886 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -361,8 +361,34 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { + unsigned long val; + + preempt_disable(); + val = gcsr_read(LOONGARCH_CSR_CRMD); + preempt_enable(); + + return (val & CSR_PRMD_PPLV) == 0; +} + +#ifdef CONFIG_GUEST_PERF_EVENTS +unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) +{ + return vcpu->arch.pc; +} + +/* + * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, + * arrived in guest context. For loongarch64, if pmu is not passthrough to VM, + * any event that arrives while a vCPU is loaded is considered to be "in guest". + */ +bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu) +{ + if (vcpu && !(vcpu->arch.aux_inuse & KVM_LARCH_PMU)) + return true; + return false; } +#endif bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) { -- 2.39.3