1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Arch specific functions for perf kvm stat.
4 *
5 * Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd.
6 *
7 */
8 #include <errno.h>
9 #include <memory.h>
10 #include "../evsel.h"
11 #include "../kvm-stat.h"
12 #include "riscv_trap_types.h"
13 #include "debug.h"
14
15 define_exit_reasons_table(riscv_exit_reasons, kvm_riscv_trap_class);
16
17 static const char * const __kvm_events_tp[] = {
18 "kvm:kvm_entry",
19 "kvm:kvm_exit",
20 NULL,
21 };
22
event_get_key(struct evsel * evsel,struct perf_sample * sample,struct event_key * key)23 static void event_get_key(struct evsel *evsel,
24 struct perf_sample *sample,
25 struct event_key *key)
26 {
27 int xlen = 64; // TODO: 32-bit support.
28
29 key->info = 0;
30 key->key = evsel__intval(evsel, sample, kvm_exit_reason(EM_RISCV)) & ~CAUSE_IRQ_FLAG(xlen);
31 key->exit_reasons = riscv_exit_reasons;
32 }
33
event_begin(struct evsel * evsel,struct perf_sample * sample __maybe_unused,struct event_key * key __maybe_unused)34 static bool event_begin(struct evsel *evsel,
35 struct perf_sample *sample __maybe_unused,
36 struct event_key *key __maybe_unused)
37 {
38 return evsel__name_is(evsel, kvm_entry_trace(EM_RISCV));
39 }
40
event_end(struct evsel * evsel,struct perf_sample * sample,struct event_key * key)41 static bool event_end(struct evsel *evsel,
42 struct perf_sample *sample,
43 struct event_key *key)
44 {
45 if (evsel__name_is(evsel, kvm_exit_trace(EM_RISCV))) {
46 event_get_key(evsel, sample, key);
47 return true;
48 }
49 return false;
50 }
51
52 static const struct kvm_events_ops exit_events = {
53 .is_begin_event = event_begin,
54 .is_end_event = event_end,
55 .decode_key = exit_event_decode_key,
56 .name = "VM-EXIT"
57 };
58
59 static const struct kvm_reg_events_ops __kvm_reg_events_ops[] = {
60 {
61 .name = "vmexit",
62 .ops = &exit_events,
63 },
64 { NULL, NULL },
65 };
66
67 static const char * const __kvm_skip_events[] = {
68 NULL,
69 };
70
__cpu_isa_init_riscv(struct perf_kvm_stat * kvm)71 int __cpu_isa_init_riscv(struct perf_kvm_stat *kvm)
72 {
73 kvm->exit_reasons_isa = "riscv64";
74 return 0;
75 }
76
__kvm_events_tp_riscv(void)77 const char * const *__kvm_events_tp_riscv(void)
78 {
79 return __kvm_events_tp;
80 }
81
__kvm_reg_events_ops_riscv(void)82 const struct kvm_reg_events_ops *__kvm_reg_events_ops_riscv(void)
83 {
84 return __kvm_reg_events_ops;
85 }
86
__kvm_skip_events_riscv(void)87 const char * const *__kvm_skip_events_riscv(void)
88 {
89 return __kvm_skip_events;
90 }
91