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/pmu.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 int decode_str_len = 40;
18 const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
19 const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
20 
21 define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
22 define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
23 
24 /* Tracepoints specific to ppc_book3s_hv */
25 const char *ppc_book3s_hv_kvm_tp[] = {
26 	"kvm_hv:kvm_guest_enter",
27 	"kvm_hv:kvm_guest_exit",
28 	"kvm_hv:kvm_hcall_enter",
29 	"kvm_hv:kvm_hcall_exit",
30 	NULL,
31 };
32 
33 /* 1 extra placeholder for NULL */
34 const char *kvm_events_tp[NR_TPS + 1];
35 const char *kvm_exit_reason;
36 
37 static void hcall_event_get_key(struct evsel *evsel,
38 				struct perf_sample *sample,
39 				struct event_key *key)
40 {
41 	key->info = 0;
42 	key->key = evsel__intval(evsel, sample, "req");
43 }
44 
45 static const char *get_hcall_exit_reason(u64 exit_code)
46 {
47 	struct exit_reasons_table *tbl = hcall_reasons;
48 
49 	while (tbl->reason != NULL) {
50 		if (tbl->exit_code == exit_code)
51 			return tbl->reason;
52 		tbl++;
53 	}
54 
55 	pr_debug("Unknown hcall code: %lld\n",
56 	       (unsigned long long)exit_code);
57 	return "UNKNOWN";
58 }
59 
60 static bool hcall_event_end(struct evsel *evsel,
61 			    struct perf_sample *sample __maybe_unused,
62 			    struct event_key *key __maybe_unused)
63 {
64 	return (!strcmp(evsel->name, kvm_events_tp[3]));
65 }
66 
67 static bool hcall_event_begin(struct evsel *evsel,
68 			      struct perf_sample *sample, struct event_key *key)
69 {
70 	if (!strcmp(evsel->name, kvm_events_tp[2])) {
71 		hcall_event_get_key(evsel, sample, key);
72 		return true;
73 	}
74 
75 	return false;
76 }
77 static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
78 				   struct event_key *key,
79 				   char *decode)
80 {
81 	const char *hcall_reason = get_hcall_exit_reason(key->key);
82 
83 	scnprintf(decode, decode_str_len, "%s", hcall_reason);
84 }
85 
86 static struct kvm_events_ops hcall_events = {
87 	.is_begin_event = hcall_event_begin,
88 	.is_end_event = hcall_event_end,
89 	.decode_key = hcall_event_decode_key,
90 	.name = "HCALL-EVENT",
91 };
92 
93 static struct kvm_events_ops exit_events = {
94 	.is_begin_event = exit_event_begin,
95 	.is_end_event = exit_event_end,
96 	.decode_key = exit_event_decode_key,
97 	.name = "VM-EXIT"
98 };
99 
100 struct kvm_reg_events_ops kvm_reg_events_ops[] = {
101 	{ .name = "vmexit", .ops = &exit_events },
102 	{ .name = "hcall", .ops = &hcall_events },
103 	{ NULL, NULL },
104 };
105 
106 const char * const kvm_skip_events[] = {
107 	NULL,
108 };
109 
110 
111 static int is_tracepoint_available(const char *str, struct evlist *evlist)
112 {
113 	struct parse_events_error err;
114 	int ret;
115 
116 	bzero(&err, sizeof(err));
117 	ret = parse_events(evlist, str, &err);
118 	if (err.str)
119 		parse_events_print_error(&err, "tracepoint");
120 	return ret;
121 }
122 
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 */
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 
160 int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
161 {
162 	return ppc__setup_kvm_tp(kvm);
163 }
164 
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  */
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 (pmu_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