1 // SPDX-License-Identifier: GPL-2.0
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"
10 #include "book3s_hv_exits.h"
11 #include "book3s_hcalls.h"
12 #include <subcmd/parse-options.h>
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";
21 define_exit_reasons_table(hv_exit_reasons
, kvm_trace_symbol_exit
);
22 define_exit_reasons_table(hcall_reasons
, kvm_trace_symbol_hcall
);
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",
33 /* 1 extra placeholder for NULL */
34 const char *kvm_events_tp
[NR_TPS
+ 1];
35 const char *kvm_exit_reason
;
37 static void hcall_event_get_key(struct evsel
*evsel
,
38 struct perf_sample
*sample
,
39 struct event_key
*key
)
42 key
->key
= evsel__intval(evsel
, sample
, "req");
45 static const char *get_hcall_exit_reason(u64 exit_code
)
47 struct exit_reasons_table
*tbl
= hcall_reasons
;
49 while (tbl
->reason
!= NULL
) {
50 if (tbl
->exit_code
== exit_code
)
55 pr_debug("Unknown hcall code: %lld\n",
56 (unsigned long long)exit_code
);
60 static bool hcall_event_end(struct evsel
*evsel
,
61 struct perf_sample
*sample __maybe_unused
,
62 struct event_key
*key __maybe_unused
)
64 return (!strcmp(evsel
->name
, kvm_events_tp
[3]));
67 static bool hcall_event_begin(struct evsel
*evsel
,
68 struct perf_sample
*sample
, struct event_key
*key
)
70 if (!strcmp(evsel
->name
, kvm_events_tp
[2])) {
71 hcall_event_get_key(evsel
, sample
, key
);
77 static void hcall_event_decode_key(struct perf_kvm_stat
*kvm __maybe_unused
,
78 struct event_key
*key
,
81 const char *hcall_reason
= get_hcall_exit_reason(key
->key
);
83 scnprintf(decode
, decode_str_len
, "%s", hcall_reason
);
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",
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
,
100 struct kvm_reg_events_ops kvm_reg_events_ops
[] = {
101 { .name
= "vmexit", .ops
= &exit_events
},
102 { .name
= "hcall", .ops
= &hcall_events
},
106 const char * const kvm_skip_events
[] = {
111 static int is_tracepoint_available(const char *str
, struct evlist
*evlist
)
113 struct parse_events_error err
;
116 bzero(&err
, sizeof(err
));
117 ret
= parse_events(evlist
, str
, &err
);
119 parse_events_print_error(&err
, "tracepoint");
123 static int ppc__setup_book3s_hv(struct perf_kvm_stat
*kvm
,
124 struct evlist
*evlist
)
126 const char **events_ptr
;
127 int i
, nr_tp
= 0, err
= -1;
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
);
137 for (i
= 0; i
< nr_tp
; i
++)
138 kvm_events_tp
[i
] = ppc_book3s_hv_kvm_tp
[i
];
140 kvm_events_tp
[i
] = NULL
;
141 kvm_exit_reason
= "trap";
142 kvm
->exit_reasons
= hv_exit_reasons
;
143 kvm
->exit_reasons_isa
= "HV";
148 /* Wrapper to setup kvm tracepoints */
149 static int ppc__setup_kvm_tp(struct perf_kvm_stat
*kvm
)
151 struct evlist
*evlist
= evlist__new();
156 /* Right now, only supported on book3s_hv */
157 return ppc__setup_book3s_hv(kvm
, evlist
);
160 int setup_kvm_events_tp(struct perf_kvm_stat
*kvm
)
162 return ppc__setup_kvm_tp(kvm
);
165 int cpu_isa_init(struct perf_kvm_stat
*kvm
, const char *cpuid __maybe_unused
)
169 ret
= ppc__setup_kvm_tp(kvm
);
171 kvm
->exit_reasons
= NULL
;
172 kvm
->exit_reasons_isa
= NULL
;
179 * Incase 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.
185 * Function to parse the arguments and return appropriate values.
187 int kvm_add_default_arch_event(int *argc
, const char **argv
)
193 const struct option event_options
[] = {
194 OPT_BOOLEAN('e', "event", &event
, NULL
),
198 tmp
= calloc(j
+ 1, sizeof(char *));
202 for (i
= 0; i
< j
; i
++)
205 parse_options(j
, tmp
, event_options
, NULL
, PARSE_OPT_KEEP_UNKNOWN
);
207 if (pmu_have_event("trace_imc", "trace_cycles")) {
208 argv
[j
++] = strdup("-e");
209 argv
[j
++] = strdup("trace_imc/trace_cycles/");