0001
0002 #include <errno.h>
0003 #include "util/kvm-stat.h"
0004 #include "util/parse-events.h"
0005 #include "util/debug.h"
0006 #include "util/evsel.h"
0007 #include "util/evlist.h"
0008 #include "util/pmu.h"
0009
0010 #include "book3s_hv_exits.h"
0011 #include "book3s_hcalls.h"
0012 #include <subcmd/parse-options.h>
0013
0014 #define NR_TPS 4
0015
0016 const char *vcpu_id_str = "vcpu_id";
0017 const int decode_str_len = 40;
0018 const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
0019 const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
0020
0021 define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
0022 define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
0023
0024
0025 const char *ppc_book3s_hv_kvm_tp[] = {
0026 "kvm_hv:kvm_guest_enter",
0027 "kvm_hv:kvm_guest_exit",
0028 "kvm_hv:kvm_hcall_enter",
0029 "kvm_hv:kvm_hcall_exit",
0030 NULL,
0031 };
0032
0033
0034 const char *kvm_events_tp[NR_TPS + 1];
0035 const char *kvm_exit_reason;
0036
0037 static void hcall_event_get_key(struct evsel *evsel,
0038 struct perf_sample *sample,
0039 struct event_key *key)
0040 {
0041 key->info = 0;
0042 key->key = evsel__intval(evsel, sample, "req");
0043 }
0044
0045 static const char *get_hcall_exit_reason(u64 exit_code)
0046 {
0047 struct exit_reasons_table *tbl = hcall_reasons;
0048
0049 while (tbl->reason != NULL) {
0050 if (tbl->exit_code == exit_code)
0051 return tbl->reason;
0052 tbl++;
0053 }
0054
0055 pr_debug("Unknown hcall code: %lld\n",
0056 (unsigned long long)exit_code);
0057 return "UNKNOWN";
0058 }
0059
0060 static bool hcall_event_end(struct evsel *evsel,
0061 struct perf_sample *sample __maybe_unused,
0062 struct event_key *key __maybe_unused)
0063 {
0064 return (!strcmp(evsel->name, kvm_events_tp[3]));
0065 }
0066
0067 static bool hcall_event_begin(struct evsel *evsel,
0068 struct perf_sample *sample, struct event_key *key)
0069 {
0070 if (!strcmp(evsel->name, kvm_events_tp[2])) {
0071 hcall_event_get_key(evsel, sample, key);
0072 return true;
0073 }
0074
0075 return false;
0076 }
0077 static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
0078 struct event_key *key,
0079 char *decode)
0080 {
0081 const char *hcall_reason = get_hcall_exit_reason(key->key);
0082
0083 scnprintf(decode, decode_str_len, "%s", hcall_reason);
0084 }
0085
0086 static struct kvm_events_ops hcall_events = {
0087 .is_begin_event = hcall_event_begin,
0088 .is_end_event = hcall_event_end,
0089 .decode_key = hcall_event_decode_key,
0090 .name = "HCALL-EVENT",
0091 };
0092
0093 static struct kvm_events_ops exit_events = {
0094 .is_begin_event = exit_event_begin,
0095 .is_end_event = exit_event_end,
0096 .decode_key = exit_event_decode_key,
0097 .name = "VM-EXIT"
0098 };
0099
0100 struct kvm_reg_events_ops kvm_reg_events_ops[] = {
0101 { .name = "vmexit", .ops = &exit_events },
0102 { .name = "hcall", .ops = &hcall_events },
0103 { NULL, NULL },
0104 };
0105
0106 const char * const kvm_skip_events[] = {
0107 NULL,
0108 };
0109
0110
0111 static int is_tracepoint_available(const char *str, struct evlist *evlist)
0112 {
0113 struct parse_events_error err;
0114 int ret;
0115
0116 parse_events_error__init(&err);
0117 ret = parse_events(evlist, str, &err);
0118 if (err.str)
0119 parse_events_error__print(&err, "tracepoint");
0120 parse_events_error__exit(&err);
0121 return ret;
0122 }
0123
0124 static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
0125 struct evlist *evlist)
0126 {
0127 const char **events_ptr;
0128 int i, nr_tp = 0, err = -1;
0129
0130
0131 for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
0132 err = is_tracepoint_available(*events_ptr, evlist);
0133 if (err)
0134 return -1;
0135 nr_tp++;
0136 }
0137
0138 for (i = 0; i < nr_tp; i++)
0139 kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];
0140
0141 kvm_events_tp[i] = NULL;
0142 kvm_exit_reason = "trap";
0143 kvm->exit_reasons = hv_exit_reasons;
0144 kvm->exit_reasons_isa = "HV";
0145
0146 return 0;
0147 }
0148
0149
0150 static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
0151 {
0152 struct evlist *evlist = evlist__new();
0153
0154 if (evlist == NULL)
0155 return -ENOMEM;
0156
0157
0158 return ppc__setup_book3s_hv(kvm, evlist);
0159 }
0160
0161 int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
0162 {
0163 return ppc__setup_kvm_tp(kvm);
0164 }
0165
0166 int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
0167 {
0168 int ret;
0169
0170 ret = ppc__setup_kvm_tp(kvm);
0171 if (ret) {
0172 kvm->exit_reasons = NULL;
0173 kvm->exit_reasons_isa = NULL;
0174 }
0175
0176 return ret;
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 int kvm_add_default_arch_event(int *argc, const char **argv)
0189 {
0190 const char **tmp;
0191 bool event = false;
0192 int i, j = *argc;
0193
0194 const struct option event_options[] = {
0195 OPT_BOOLEAN('e', "event", &event, NULL),
0196 OPT_END()
0197 };
0198
0199 tmp = calloc(j + 1, sizeof(char *));
0200 if (!tmp)
0201 return -EINVAL;
0202
0203 for (i = 0; i < j; i++)
0204 tmp[i] = argv[i];
0205
0206 parse_options(j, tmp, event_options, NULL, PARSE_OPT_KEEP_UNKNOWN);
0207 if (!event) {
0208 if (pmu_have_event("trace_imc", "trace_cycles")) {
0209 argv[j++] = strdup("-e");
0210 argv[j++] = strdup("trace_imc/trace_cycles/");
0211 *argc += 2;
0212 } else {
0213 free(tmp);
0214 return -EINVAL;
0215 }
0216 }
0217
0218 free(tmp);
0219 return 0;
0220 }