1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a43783aeSArnaldo Carvalho de Melo #include <errno.h>
3066d3593SHemant Kumar #include "util/kvm-stat.h"
4066d3593SHemant Kumar #include "util/parse-events.h"
578e6c39bSHemant Kumar #include "util/debug.h"
65691903aSArnaldo Carvalho de Melo #include "util/evsel.h"
75afbb37cSArnaldo Carvalho de Melo #include "util/evlist.h"
82bff2b82SAnju T Sudhakar #include "util/pmus.h"
9066d3593SHemant Kumar
10066d3593SHemant Kumar #include "book3s_hv_exits.h"
1178e6c39bSHemant Kumar #include "book3s_hcalls.h"
12124eb5f8SAnju T Sudhakar #include <subcmd/parse-options.h>
13066d3593SHemant Kumar
1478e6c39bSHemant Kumar #define NR_TPS 4
15066d3593SHemant Kumar
16066d3593SHemant Kumar const char *vcpu_id_str = "vcpu_id";
17066d3593SHemant Kumar const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
18066d3593SHemant Kumar const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
19066d3593SHemant Kumar
20066d3593SHemant Kumar define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
2178e6c39bSHemant Kumar define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
22066d3593SHemant Kumar
23066d3593SHemant Kumar /* Tracepoints specific to ppc_book3s_hv */
24066d3593SHemant Kumar const char *ppc_book3s_hv_kvm_tp[] = {
25066d3593SHemant Kumar "kvm_hv:kvm_guest_enter",
26066d3593SHemant Kumar "kvm_hv:kvm_guest_exit",
2778e6c39bSHemant Kumar "kvm_hv:kvm_hcall_enter",
2878e6c39bSHemant Kumar "kvm_hv:kvm_hcall_exit",
2978e6c39bSHemant Kumar NULL,
30066d3593SHemant Kumar };
31066d3593SHemant Kumar
32066d3593SHemant Kumar /* 1 extra placeholder for NULL */
33066d3593SHemant Kumar const char *kvm_events_tp[NR_TPS + 1];
34066d3593SHemant Kumar const char *kvm_exit_reason;
35066d3593SHemant Kumar
hcall_event_get_key(struct evsel * evsel,struct perf_sample * sample,struct event_key * key)3632dcd021SJiri Olsa static void hcall_event_get_key(struct evsel *evsel,
3778e6c39bSHemant Kumar struct perf_sample *sample,
3878e6c39bSHemant Kumar struct event_key *key)
3978e6c39bSHemant Kumar {
4078e6c39bSHemant Kumar key->info = 0;
41efc0cdc9SArnaldo Carvalho de Melo key->key = evsel__intval(evsel, sample, "req");
4278e6c39bSHemant Kumar }
4378e6c39bSHemant Kumar
get_hcall_exit_reason(u64 exit_code)4478e6c39bSHemant Kumar static const char *get_hcall_exit_reason(u64 exit_code)
4578e6c39bSHemant Kumar {
4678e6c39bSHemant Kumar struct exit_reasons_table *tbl = hcall_reasons;
4778e6c39bSHemant Kumar
4878e6c39bSHemant Kumar while (tbl->reason != NULL) {
4978e6c39bSHemant Kumar if (tbl->exit_code == exit_code)
5078e6c39bSHemant Kumar return tbl->reason;
5178e6c39bSHemant Kumar tbl++;
5278e6c39bSHemant Kumar }
5378e6c39bSHemant Kumar
5478e6c39bSHemant Kumar pr_debug("Unknown hcall code: %lld\n",
5578e6c39bSHemant Kumar (unsigned long long)exit_code);
5678e6c39bSHemant Kumar return "UNKNOWN";
5778e6c39bSHemant Kumar }
5878e6c39bSHemant Kumar
hcall_event_end(struct evsel * evsel,struct perf_sample * sample __maybe_unused,struct event_key * key __maybe_unused)5932dcd021SJiri Olsa static bool hcall_event_end(struct evsel *evsel,
6078e6c39bSHemant Kumar struct perf_sample *sample __maybe_unused,
6178e6c39bSHemant Kumar struct event_key *key __maybe_unused)
6278e6c39bSHemant Kumar {
63*ce1d3bc2SArnaldo Carvalho de Melo return (evsel__name_is(evsel, kvm_events_tp[3]));
6478e6c39bSHemant Kumar }
6578e6c39bSHemant Kumar
hcall_event_begin(struct evsel * evsel,struct perf_sample * sample,struct event_key * key)6632dcd021SJiri Olsa static bool hcall_event_begin(struct evsel *evsel,
6778e6c39bSHemant Kumar struct perf_sample *sample, struct event_key *key)
6878e6c39bSHemant Kumar {
69*ce1d3bc2SArnaldo Carvalho de Melo if (evsel__name_is(evsel, kvm_events_tp[2])) {
7078e6c39bSHemant Kumar hcall_event_get_key(evsel, sample, key);
7178e6c39bSHemant Kumar return true;
7278e6c39bSHemant Kumar }
7378e6c39bSHemant Kumar
7478e6c39bSHemant Kumar return false;
7578e6c39bSHemant Kumar }
hcall_event_decode_key(struct perf_kvm_stat * kvm __maybe_unused,struct event_key * key,char * decode)7678e6c39bSHemant Kumar static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
7778e6c39bSHemant Kumar struct event_key *key,
7878e6c39bSHemant Kumar char *decode)
7978e6c39bSHemant Kumar {
8078e6c39bSHemant Kumar const char *hcall_reason = get_hcall_exit_reason(key->key);
8178e6c39bSHemant Kumar
822d31e0bfSLeo Yan scnprintf(decode, KVM_EVENT_NAME_LEN, "%s", hcall_reason);
8378e6c39bSHemant Kumar }
8478e6c39bSHemant Kumar
8578e6c39bSHemant Kumar static struct kvm_events_ops hcall_events = {
8678e6c39bSHemant Kumar .is_begin_event = hcall_event_begin,
8778e6c39bSHemant Kumar .is_end_event = hcall_event_end,
8878e6c39bSHemant Kumar .decode_key = hcall_event_decode_key,
8978e6c39bSHemant Kumar .name = "HCALL-EVENT",
9078e6c39bSHemant Kumar };
9178e6c39bSHemant Kumar
92066d3593SHemant Kumar static struct kvm_events_ops exit_events = {
93066d3593SHemant Kumar .is_begin_event = exit_event_begin,
94066d3593SHemant Kumar .is_end_event = exit_event_end,
95066d3593SHemant Kumar .decode_key = exit_event_decode_key,
96066d3593SHemant Kumar .name = "VM-EXIT"
97066d3593SHemant Kumar };
98066d3593SHemant Kumar
99066d3593SHemant Kumar struct kvm_reg_events_ops kvm_reg_events_ops[] = {
100066d3593SHemant Kumar { .name = "vmexit", .ops = &exit_events },
10178e6c39bSHemant Kumar { .name = "hcall", .ops = &hcall_events },
102066d3593SHemant Kumar { NULL, NULL },
103066d3593SHemant Kumar };
104066d3593SHemant Kumar
105066d3593SHemant Kumar const char * const kvm_skip_events[] = {
106066d3593SHemant Kumar NULL,
107066d3593SHemant Kumar };
108066d3593SHemant Kumar
109066d3593SHemant Kumar
is_tracepoint_available(const char * str,struct evlist * evlist)11063503dbaSJiri Olsa static int is_tracepoint_available(const char *str, struct evlist *evlist)
111066d3593SHemant Kumar {
112066d3593SHemant Kumar struct parse_events_error err;
113066d3593SHemant Kumar int ret;
114066d3593SHemant Kumar
11507eafd4eSIan Rogers parse_events_error__init(&err);
116066d3593SHemant Kumar ret = parse_events(evlist, str, &err);
117066d3593SHemant Kumar if (err.str)
1186c191289SIan Rogers parse_events_error__print(&err, "tracepoint");
11907eafd4eSIan Rogers parse_events_error__exit(&err);
120066d3593SHemant Kumar return ret;
121066d3593SHemant Kumar }
122066d3593SHemant Kumar
ppc__setup_book3s_hv(struct perf_kvm_stat * kvm,struct evlist * evlist)123066d3593SHemant Kumar static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
12463503dbaSJiri Olsa struct evlist *evlist)
125066d3593SHemant Kumar {
126066d3593SHemant Kumar const char **events_ptr;
127066d3593SHemant Kumar int i, nr_tp = 0, err = -1;
128066d3593SHemant Kumar
129066d3593SHemant Kumar /* Check for book3s_hv tracepoints */
130066d3593SHemant Kumar for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
131066d3593SHemant Kumar err = is_tracepoint_available(*events_ptr, evlist);
132066d3593SHemant Kumar if (err)
133066d3593SHemant Kumar return -1;
134066d3593SHemant Kumar nr_tp++;
135066d3593SHemant Kumar }
136066d3593SHemant Kumar
137066d3593SHemant Kumar for (i = 0; i < nr_tp; i++)
138066d3593SHemant Kumar kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];
139066d3593SHemant Kumar
140066d3593SHemant Kumar kvm_events_tp[i] = NULL;
141066d3593SHemant Kumar kvm_exit_reason = "trap";
142066d3593SHemant Kumar kvm->exit_reasons = hv_exit_reasons;
143066d3593SHemant Kumar kvm->exit_reasons_isa = "HV";
144066d3593SHemant Kumar
145066d3593SHemant Kumar return 0;
146066d3593SHemant Kumar }
147066d3593SHemant Kumar
148066d3593SHemant Kumar /* Wrapper to setup kvm tracepoints */
ppc__setup_kvm_tp(struct perf_kvm_stat * kvm)149066d3593SHemant Kumar static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
150066d3593SHemant Kumar {
1510f98b11cSJiri Olsa struct evlist *evlist = evlist__new();
152066d3593SHemant Kumar
153066d3593SHemant Kumar if (evlist == NULL)
154066d3593SHemant Kumar return -ENOMEM;
155066d3593SHemant Kumar
156066d3593SHemant Kumar /* Right now, only supported on book3s_hv */
157066d3593SHemant Kumar return ppc__setup_book3s_hv(kvm, evlist);
158066d3593SHemant Kumar }
159066d3593SHemant Kumar
setup_kvm_events_tp(struct perf_kvm_stat * kvm)160066d3593SHemant Kumar int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
161066d3593SHemant Kumar {
162066d3593SHemant Kumar return ppc__setup_kvm_tp(kvm);
163066d3593SHemant Kumar }
164066d3593SHemant Kumar
cpu_isa_init(struct perf_kvm_stat * kvm,const char * cpuid __maybe_unused)165066d3593SHemant Kumar int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
166066d3593SHemant Kumar {
167066d3593SHemant Kumar int ret;
168066d3593SHemant Kumar
169066d3593SHemant Kumar ret = ppc__setup_kvm_tp(kvm);
170066d3593SHemant Kumar if (ret) {
171066d3593SHemant Kumar kvm->exit_reasons = NULL;
172066d3593SHemant Kumar kvm->exit_reasons_isa = NULL;
173066d3593SHemant Kumar }
174066d3593SHemant Kumar
175066d3593SHemant Kumar return ret;
176066d3593SHemant Kumar }
177124eb5f8SAnju T Sudhakar
178124eb5f8SAnju T Sudhakar /*
179124eb5f8SAnju T Sudhakar * In case of powerpc architecture, pmu registers are programmable
180124eb5f8SAnju T Sudhakar * by guest kernel. So monitoring guest via host may not provide
1812bff2b82SAnju T Sudhakar * valid samples with default 'cycles' event. It is better to use
1822bff2b82SAnju T Sudhakar * 'trace_imc/trace_cycles' event for guest profiling, since it
1832bff2b82SAnju T Sudhakar * can track the guest instruction pointer in the trace-record.
184124eb5f8SAnju T Sudhakar *
185124eb5f8SAnju T Sudhakar * Function to parse the arguments and return appropriate values.
186124eb5f8SAnju T Sudhakar */
kvm_add_default_arch_event(int * argc,const char ** argv)187124eb5f8SAnju T Sudhakar int kvm_add_default_arch_event(int *argc, const char **argv)
188124eb5f8SAnju T Sudhakar {
189124eb5f8SAnju T Sudhakar const char **tmp;
190124eb5f8SAnju T Sudhakar bool event = false;
191124eb5f8SAnju T Sudhakar int i, j = *argc;
192124eb5f8SAnju T Sudhakar
193124eb5f8SAnju T Sudhakar const struct option event_options[] = {
194124eb5f8SAnju T Sudhakar OPT_BOOLEAN('e', "event", &event, NULL),
195124eb5f8SAnju T Sudhakar OPT_END()
196124eb5f8SAnju T Sudhakar };
197124eb5f8SAnju T Sudhakar
198124eb5f8SAnju T Sudhakar tmp = calloc(j + 1, sizeof(char *));
199124eb5f8SAnju T Sudhakar if (!tmp)
200124eb5f8SAnju T Sudhakar return -EINVAL;
201124eb5f8SAnju T Sudhakar
202124eb5f8SAnju T Sudhakar for (i = 0; i < j; i++)
203124eb5f8SAnju T Sudhakar tmp[i] = argv[i];
204124eb5f8SAnju T Sudhakar
205124eb5f8SAnju T Sudhakar parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
206124eb5f8SAnju T Sudhakar if (!event) {
2072bff2b82SAnju T Sudhakar if (perf_pmus__have_event("trace_imc", "trace_cycles")) {
2082bff2b82SAnju T Sudhakar argv[j++] = strdup("-e");
2092bff2b82SAnju T Sudhakar argv[j++] = strdup("trace_imc/trace_cycles/");
2102bff2b82SAnju T Sudhakar *argc += 2;
2112bff2b82SAnju T Sudhakar } else {
212124eb5f8SAnju T Sudhakar free(tmp);
213124eb5f8SAnju T Sudhakar return -EINVAL;
214124eb5f8SAnju T Sudhakar }
2152bff2b82SAnju T Sudhakar }
216124eb5f8SAnju T Sudhakar
217124eb5f8SAnju T Sudhakar free(tmp);
218124eb5f8SAnju T Sudhakar return 0;
219124eb5f8SAnju T Sudhakar }
220