1 // SPDX-License-Identifier: GPL-2.0
3 #include "util/kvm-stat.h"
4 #include "util/parse-events.h"
5 #include "util/debug.h"
6 #include "util/evsel.h"
7 #include "util/evlist.h"
9 #include "book3s_hv_exits.h"
10 #include "book3s_hcalls.h"
14 const char *vcpu_id_str
= "vcpu_id";
15 const int decode_str_len
= 40;
16 const char *kvm_entry_trace
= "kvm_hv:kvm_guest_enter";
17 const char *kvm_exit_trace
= "kvm_hv:kvm_guest_exit";
19 define_exit_reasons_table(hv_exit_reasons
, kvm_trace_symbol_exit
);
20 define_exit_reasons_table(hcall_reasons
, kvm_trace_symbol_hcall
);
22 /* Tracepoints specific to ppc_book3s_hv */
23 const char *ppc_book3s_hv_kvm_tp
[] = {
24 "kvm_hv:kvm_guest_enter",
25 "kvm_hv:kvm_guest_exit",
26 "kvm_hv:kvm_hcall_enter",
27 "kvm_hv:kvm_hcall_exit",
31 /* 1 extra placeholder for NULL */
32 const char *kvm_events_tp
[NR_TPS
+ 1];
33 const char *kvm_exit_reason
;
35 static void hcall_event_get_key(struct perf_evsel
*evsel
,
36 struct perf_sample
*sample
,
37 struct event_key
*key
)
40 key
->key
= perf_evsel__intval(evsel
, sample
, "req");
43 static const char *get_hcall_exit_reason(u64 exit_code
)
45 struct exit_reasons_table
*tbl
= hcall_reasons
;
47 while (tbl
->reason
!= NULL
) {
48 if (tbl
->exit_code
== exit_code
)
53 pr_debug("Unknown hcall code: %lld\n",
54 (unsigned long long)exit_code
);
58 static bool hcall_event_end(struct perf_evsel
*evsel
,
59 struct perf_sample
*sample __maybe_unused
,
60 struct event_key
*key __maybe_unused
)
62 return (!strcmp(evsel
->name
, kvm_events_tp
[3]));
65 static bool hcall_event_begin(struct perf_evsel
*evsel
,
66 struct perf_sample
*sample
, struct event_key
*key
)
68 if (!strcmp(evsel
->name
, kvm_events_tp
[2])) {
69 hcall_event_get_key(evsel
, sample
, key
);
75 static void hcall_event_decode_key(struct perf_kvm_stat
*kvm __maybe_unused
,
76 struct event_key
*key
,
79 const char *hcall_reason
= get_hcall_exit_reason(key
->key
);
81 scnprintf(decode
, decode_str_len
, "%s", hcall_reason
);
84 static struct kvm_events_ops hcall_events
= {
85 .is_begin_event
= hcall_event_begin
,
86 .is_end_event
= hcall_event_end
,
87 .decode_key
= hcall_event_decode_key
,
88 .name
= "HCALL-EVENT",
91 static struct kvm_events_ops exit_events
= {
92 .is_begin_event
= exit_event_begin
,
93 .is_end_event
= exit_event_end
,
94 .decode_key
= exit_event_decode_key
,
98 struct kvm_reg_events_ops kvm_reg_events_ops
[] = {
99 { .name
= "vmexit", .ops
= &exit_events
},
100 { .name
= "hcall", .ops
= &hcall_events
},
104 const char * const kvm_skip_events
[] = {
109 static int is_tracepoint_available(const char *str
, struct perf_evlist
*evlist
)
111 struct parse_events_error err
;
115 ret
= parse_events(evlist
, str
, &err
);
117 pr_err("%s : %s\n", str
, err
.str
);
121 static int ppc__setup_book3s_hv(struct perf_kvm_stat
*kvm
,
122 struct perf_evlist
*evlist
)
124 const char **events_ptr
;
125 int i
, nr_tp
= 0, err
= -1;
127 /* Check for book3s_hv tracepoints */
128 for (events_ptr
= ppc_book3s_hv_kvm_tp
; *events_ptr
; events_ptr
++) {
129 err
= is_tracepoint_available(*events_ptr
, evlist
);
135 for (i
= 0; i
< nr_tp
; i
++)
136 kvm_events_tp
[i
] = ppc_book3s_hv_kvm_tp
[i
];
138 kvm_events_tp
[i
] = NULL
;
139 kvm_exit_reason
= "trap";
140 kvm
->exit_reasons
= hv_exit_reasons
;
141 kvm
->exit_reasons_isa
= "HV";
146 /* Wrapper to setup kvm tracepoints */
147 static int ppc__setup_kvm_tp(struct perf_kvm_stat
*kvm
)
149 struct perf_evlist
*evlist
= perf_evlist__new();
154 /* Right now, only supported on book3s_hv */
155 return ppc__setup_book3s_hv(kvm
, evlist
);
158 int setup_kvm_events_tp(struct perf_kvm_stat
*kvm
)
160 return ppc__setup_kvm_tp(kvm
);
163 int cpu_isa_init(struct perf_kvm_stat
*kvm
, const char *cpuid __maybe_unused
)
167 ret
= ppc__setup_kvm_tp(kvm
);
169 kvm
->exit_reasons
= NULL
;
170 kvm
->exit_reasons_isa
= NULL
;