@@ -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);
@@ -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)
{
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@loongson.cn> --- arch/loongarch/include/asm/kvm_host.h | 2 ++ arch/loongarch/kvm/vcpu.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+)