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