1 #include <errno.h>
2 #include "util/kvm-stat.h"
3 #include "util/parse-events.h"
4 #include "util/debug.h"
5 
6 #include "book3s_hv_exits.h"
7 #include "book3s_hcalls.h"
8 
9 #define NR_TPS 4
10 
11 const char *vcpu_id_str = "vcpu_id";
12 const int decode_str_len = 40;
13 const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
14 const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
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 const char *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 const char *kvm_events_tp[NR_TPS + 1];
30 const char *kvm_exit_reason;
31 
32 static void hcall_event_get_key(struct perf_evsel *evsel,
33 				struct perf_sample *sample,
34 				struct event_key *key)
35 {
36 	key->info = 0;
37 	key->key = perf_evsel__intval(evsel, sample, "req");
38 }
39 
40 static const char *get_hcall_exit_reason(u64 exit_code)
41 {
42 	struct exit_reasons_table *tbl = hcall_reasons;
43 
44 	while (tbl->reason != NULL) {
45 		if (tbl->exit_code == exit_code)
46 			return tbl->reason;
47 		tbl++;
48 	}
49 
50 	pr_debug("Unknown hcall code: %lld\n",
51 	       (unsigned long long)exit_code);
52 	return "UNKNOWN";
53 }
54 
55 static bool hcall_event_end(struct perf_evsel *evsel,
56 			    struct perf_sample *sample __maybe_unused,
57 			    struct event_key *key __maybe_unused)
58 {
59 	return (!strcmp(evsel->name, kvm_events_tp[3]));
60 }
61 
62 static bool hcall_event_begin(struct perf_evsel *evsel,
63 			      struct perf_sample *sample, struct event_key *key)
64 {
65 	if (!strcmp(evsel->name, kvm_events_tp[2])) {
66 		hcall_event_get_key(evsel, sample, key);
67 		return true;
68 	}
69 
70 	return false;
71 }
72 static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
73 				   struct event_key *key,
74 				   char *decode)
75 {
76 	const char *hcall_reason = get_hcall_exit_reason(key->key);
77 
78 	scnprintf(decode, decode_str_len, "%s", hcall_reason);
79 }
80 
81 static struct kvm_events_ops hcall_events = {
82 	.is_begin_event = hcall_event_begin,
83 	.is_end_event = hcall_event_end,
84 	.decode_key = hcall_event_decode_key,
85 	.name = "HCALL-EVENT",
86 };
87 
88 static struct kvm_events_ops exit_events = {
89 	.is_begin_event = exit_event_begin,
90 	.is_end_event = exit_event_end,
91 	.decode_key = exit_event_decode_key,
92 	.name = "VM-EXIT"
93 };
94 
95 struct kvm_reg_events_ops kvm_reg_events_ops[] = {
96 	{ .name = "vmexit", .ops = &exit_events },
97 	{ .name = "hcall", .ops = &hcall_events },
98 	{ NULL, NULL },
99 };
100 
101 const char * const kvm_skip_events[] = {
102 	NULL,
103 };
104 
105 
106 static int is_tracepoint_available(const char *str, struct perf_evlist *evlist)
107 {
108 	struct parse_events_error err;
109 	int ret;
110 
111 	err.str = NULL;
112 	ret = parse_events(evlist, str, &err);
113 	if (err.str)
114 		pr_err("%s : %s\n", str, err.str);
115 	return ret;
116 }
117 
118 static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
119 				struct perf_evlist *evlist)
120 {
121 	const char **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_reason = "trap";
137 	kvm->exit_reasons = hv_exit_reasons;
138 	kvm->exit_reasons_isa = "HV";
139 
140 	return 0;
141 }
142 
143 /* Wrapper to setup kvm tracepoints */
144 static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
145 {
146 	struct perf_evlist *evlist = perf_evlist__new();
147 
148 	if (evlist == NULL)
149 		return -ENOMEM;
150 
151 	/* Right now, only supported on book3s_hv */
152 	return ppc__setup_book3s_hv(kvm, evlist);
153 }
154 
155 int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
156 {
157 	return ppc__setup_kvm_tp(kvm);
158 }
159 
160 int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
161 {
162 	int ret;
163 
164 	ret = ppc__setup_kvm_tp(kvm);
165 	if (ret) {
166 		kvm->exit_reasons = NULL;
167 		kvm->exit_reasons_isa = NULL;
168 	}
169 
170 	return ret;
171 }
172