1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on the x86 implementation.
5 * Copyright (C) 2012 ARM Ltd.
6 * Author: Marc Zyngier <marc.zyngier@arm.com>
9 #include <linux/perf_event.h>
10 #include <linux/kvm_host.h>
12 #include <asm/kvm_emulate.h>
14 DEFINE_STATIC_KEY_FALSE(kvm_arm_pmu_available);
16 static int kvm_is_in_guest(void)
18 return kvm_get_running_vcpu() != NULL;
21 static int kvm_is_user_mode(void)
23 struct kvm_vcpu *vcpu;
25 vcpu = kvm_get_running_vcpu();
28 return !vcpu_mode_priv(vcpu);
33 static unsigned long kvm_get_guest_ip(void)
35 struct kvm_vcpu *vcpu;
37 vcpu = kvm_get_running_vcpu();
40 return *vcpu_pc(vcpu);
45 static struct perf_guest_info_callbacks kvm_guest_cbs = {
46 .is_in_guest = kvm_is_in_guest,
47 .is_user_mode = kvm_is_user_mode,
48 .get_guest_ip = kvm_get_guest_ip,
51 int kvm_perf_init(void)
53 if (kvm_pmu_probe_pmuver() != 0xf && !is_protected_kvm_enabled())
54 static_branch_enable(&kvm_arm_pmu_available);
56 return perf_register_guest_info_callbacks(&kvm_guest_cbs);
59 int kvm_perf_teardown(void)
61 return perf_unregister_guest_info_callbacks(&kvm_guest_cbs);