1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include "../kvm-stat.h"
4 #include "../parse-events.h"
5 #include "../debug.h"
6 #include "../evsel.h"
7 #include "../evlist.h"
8 #include "../pmus.h"
9
10 #include "book3s_hv_exits.h"
11 #include "book3s_hcalls.h"
12 #include <subcmd/parse-options.h>
13
14 #define NR_TPS 4
15
16 define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
17 define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
18
19 /* Tracepoints specific to ppc_book3s_hv */
20 static const char * const ppc_book3s_hv_kvm_tp[] = {
21 "kvm_hv:kvm_guest_enter",
22 "kvm_hv:kvm_guest_exit",
23 "kvm_hv:kvm_hcall_enter",
24 "kvm_hv:kvm_hcall_exit",
25 NULL,
26 };
27
28 /* 1 extra placeholder for NULL */
29 static const char *__kvm_events_tp[NR_TPS + 1];
30
hcall_event_get_key(struct evsel * evsel,struct perf_sample * sample,struct event_key * key)31 static void hcall_event_get_key(struct evsel *evsel,
32 struct perf_sample *sample,
33 struct event_key *key)
34 {
35 key->info = 0;
36 key->key = evsel__intval(evsel, sample, "req");
37 }
38
get_hcall_exit_reason(u64 exit_code)39 static const char *get_hcall_exit_reason(u64 exit_code)
40 {
41 struct exit_reasons_table *tbl = hcall_reasons;
42
43 while (tbl->reason != NULL) {
44 if (tbl->exit_code == exit_code)
45 return tbl->reason;
46 tbl++;
47 }
48
49 pr_debug("Unknown hcall code: %lld\n",
50 (unsigned long long)exit_code);
51 return "UNKNOWN";
52 }
53
hcall_event_end(struct evsel * evsel,struct perf_sample * sample __maybe_unused,struct event_key * key __maybe_unused)54 static bool hcall_event_end(struct evsel *evsel,
55 struct perf_sample *sample __maybe_unused,
56 struct event_key *key __maybe_unused)
57 {
58 return evsel__name_is(evsel, __kvm_events_tp[3]);
59 }
60
hcall_event_begin(struct evsel * evsel,struct perf_sample * sample,struct event_key * key)61 static bool hcall_event_begin(struct evsel *evsel,
62 struct perf_sample *sample, struct event_key *key)
63 {
64 if (evsel__name_is(evsel, __kvm_events_tp[2])) {
65 hcall_event_get_key(evsel, sample, key);
66 return true;
67 }
68
69 return false;
70 }
hcall_event_decode_key(struct perf_kvm_stat * kvm __maybe_unused,struct event_key * key,char * decode)71 static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
72 struct event_key *key,
73 char *decode)
74 {
75 const char *hcall_reason = get_hcall_exit_reason(key->key);
76
77 scnprintf(decode, KVM_EVENT_NAME_LEN, "%s", hcall_reason);
78 }
79
80 static const struct kvm_events_ops hcall_events = {
81 .is_begin_event = hcall_event_begin,
82 .is_end_event = hcall_event_end,
83 .decode_key = hcall_event_decode_key,
84 .name = "HCALL-EVENT",
85 };
86
87 static const struct kvm_events_ops exit_events = {
88 .is_begin_event = exit_event_begin,
89 .is_end_event = exit_event_end,
90 .decode_key = exit_event_decode_key,
91 .name = "VM-EXIT"
92 };
93
94 static const struct kvm_reg_events_ops __kvm_reg_events_ops[] = {
95 { .name = "vmexit", .ops = &exit_events },
96 { .name = "hcall", .ops = &hcall_events },
97 { NULL, NULL },
98 };
99
100 static const char * const __kvm_skip_events[] = {
101 NULL,
102 };
103
104
is_tracepoint_available(const char * str,struct evlist * evlist)105 static int is_tracepoint_available(const char *str, struct evlist *evlist)
106 {
107 struct parse_events_error err;
108 int ret;
109
110 parse_events_error__init(&err);
111 ret = parse_events(evlist, str, &err);
112 if (ret)
113 parse_events_error__print(&err, "tracepoint");
114 parse_events_error__exit(&err);
115 return ret;
116 }
117
ppc__setup_book3s_hv(struct perf_kvm_stat * kvm,struct evlist * evlist)118 static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
119 struct evlist *evlist)
120 {
121 const char * const *events_ptr;
122 int i, nr_tp = 0, err = -1;
123
124 /* Check for book3s_hv tracepoints */
125 for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
126 err = is_tracepoint_available(*events_ptr, evlist);
127 if (err)
128 return -1;
129 nr_tp++;
130 }
131
132 for (i = 0; i < nr_tp; i++)
133 __kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];
134
135 __kvm_events_tp[i] = NULL;
136 kvm->exit_reasons = hv_exit_reasons;
137 kvm->exit_reasons_isa = "HV";
138
139 return 0;
140 }
141
142 /* Wrapper to setup kvm tracepoints */
ppc__setup_kvm_tp(struct perf_kvm_stat * kvm)143 static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
144 {
145 struct evlist *evlist = evlist__new();
146
147 if (evlist == NULL)
148 return -ENOMEM;
149
150 /* Right now, only supported on book3s_hv */
151 return ppc__setup_book3s_hv(kvm, evlist);
152 }
153
__setup_kvm_events_tp_powerpc(struct perf_kvm_stat * kvm)154 int __setup_kvm_events_tp_powerpc(struct perf_kvm_stat *kvm)
155 {
156 return ppc__setup_kvm_tp(kvm);
157 }
158
__cpu_isa_init_powerpc(struct perf_kvm_stat * kvm)159 int __cpu_isa_init_powerpc(struct perf_kvm_stat *kvm)
160 {
161 int ret;
162
163 ret = ppc__setup_kvm_tp(kvm);
164 if (ret) {
165 kvm->exit_reasons = NULL;
166 kvm->exit_reasons_isa = NULL;
167 }
168
169 return ret;
170 }
171
172 /*
173 * In case of powerpc architecture, pmu registers are programmable
174 * by guest kernel. So monitoring guest via host may not provide
175 * valid samples with default 'cycles' event. It is better to use
176 * 'trace_imc/trace_cycles' event for guest profiling, since it
177 * can track the guest instruction pointer in the trace-record.
178 *
179 * Function to parse the arguments and return appropriate values.
180 */
__kvm_add_default_arch_event_powerpc(int * argc,const char ** argv)181 int __kvm_add_default_arch_event_powerpc(int *argc, const char **argv)
182 {
183 const char **tmp;
184 bool event = false;
185 int i, j = *argc;
186
187 const struct option event_options[] = {
188 OPT_BOOLEAN('e', "event", &event, NULL),
189 OPT_END()
190 };
191
192 tmp = calloc(j + 1, sizeof(char *));
193 if (!tmp)
194 return -EINVAL;
195
196 for (i = 0; i < j; i++)
197 tmp[i] = argv[i];
198
199 parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
200 if (!event) {
201 if (perf_pmus__have_event("trace_imc", "trace_cycles")) {
202 argv[j++] = strdup("-e");
203 argv[j++] = strdup("trace_imc/trace_cycles/");
204 *argc += 2;
205 } else {
206 free(tmp);
207 return -EINVAL;
208 }
209 }
210
211 free(tmp);
212 return 0;
213 }
214
__kvm_events_tp_powerpc(void)215 const char * const *__kvm_events_tp_powerpc(void)
216 {
217 return __kvm_events_tp;
218 }
219
__kvm_reg_events_ops_powerpc(void)220 const struct kvm_reg_events_ops *__kvm_reg_events_ops_powerpc(void)
221 {
222 return __kvm_reg_events_ops;
223 }
224
__kvm_skip_events_powerpc(void)225 const char * const *__kvm_skip_events_powerpc(void)
226 {
227 return __kvm_skip_events;
228 }
229