Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / perf / arch / x86 / util / kvm-stat.c
blob072920475b659d106594de7a06896f31829c0609
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <string.h>
4 #include "../../../util/kvm-stat.h"
5 #include "../../../util/evsel.h"
6 #include <asm/svm.h>
7 #include <asm/vmx.h>
8 #include <asm/kvm.h>
10 define_exit_reasons_table(vmx_exit_reasons, VMX_EXIT_REASONS);
11 define_exit_reasons_table(svm_exit_reasons, SVM_EXIT_REASONS);
13 static struct kvm_events_ops exit_events = {
14 .is_begin_event = exit_event_begin,
15 .is_end_event = exit_event_end,
16 .decode_key = exit_event_decode_key,
17 .name = "VM-EXIT"
20 const char *vcpu_id_str = "vcpu_id";
21 const int decode_str_len = 20;
22 const char *kvm_exit_reason = "exit_reason";
23 const char *kvm_entry_trace = "kvm:kvm_entry";
24 const char *kvm_exit_trace = "kvm:kvm_exit";
27 * For the mmio events, we treat:
28 * the time of MMIO write: kvm_mmio(KVM_TRACE_MMIO_WRITE...) -> kvm_entry
29 * the time of MMIO read: kvm_exit -> kvm_mmio(KVM_TRACE_MMIO_READ...).
31 static void mmio_event_get_key(struct evsel *evsel, struct perf_sample *sample,
32 struct event_key *key)
34 key->key = evsel__intval(evsel, sample, "gpa");
35 key->info = evsel__intval(evsel, sample, "type");
38 #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
39 #define KVM_TRACE_MMIO_READ 1
40 #define KVM_TRACE_MMIO_WRITE 2
42 static bool mmio_event_begin(struct evsel *evsel,
43 struct perf_sample *sample, struct event_key *key)
45 /* MMIO read begin event in kernel. */
46 if (kvm_exit_event(evsel))
47 return true;
49 /* MMIO write begin event in kernel. */
50 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
51 evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_WRITE) {
52 mmio_event_get_key(evsel, sample, key);
53 return true;
56 return false;
59 static bool mmio_event_end(struct evsel *evsel, struct perf_sample *sample,
60 struct event_key *key)
62 /* MMIO write end event in kernel. */
63 if (kvm_entry_event(evsel))
64 return true;
66 /* MMIO read end event in kernel.*/
67 if (!strcmp(evsel->name, "kvm:kvm_mmio") &&
68 evsel__intval(evsel, sample, "type") == KVM_TRACE_MMIO_READ) {
69 mmio_event_get_key(evsel, sample, key);
70 return true;
73 return false;
76 static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
77 struct event_key *key,
78 char *decode)
80 scnprintf(decode, decode_str_len, "%#lx:%s",
81 (unsigned long)key->key,
82 key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
85 static struct kvm_events_ops mmio_events = {
86 .is_begin_event = mmio_event_begin,
87 .is_end_event = mmio_event_end,
88 .decode_key = mmio_event_decode_key,
89 .name = "MMIO Access"
92 /* The time of emulation pio access is from kvm_pio to kvm_entry. */
93 static void ioport_event_get_key(struct evsel *evsel,
94 struct perf_sample *sample,
95 struct event_key *key)
97 key->key = evsel__intval(evsel, sample, "port");
98 key->info = evsel__intval(evsel, sample, "rw");
101 static bool ioport_event_begin(struct evsel *evsel,
102 struct perf_sample *sample,
103 struct event_key *key)
105 if (!strcmp(evsel->name, "kvm:kvm_pio")) {
106 ioport_event_get_key(evsel, sample, key);
107 return true;
110 return false;
113 static bool ioport_event_end(struct evsel *evsel,
114 struct perf_sample *sample __maybe_unused,
115 struct event_key *key __maybe_unused)
117 return kvm_entry_event(evsel);
120 static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
121 struct event_key *key,
122 char *decode)
124 scnprintf(decode, decode_str_len, "%#llx:%s",
125 (unsigned long long)key->key,
126 key->info ? "POUT" : "PIN");
129 static struct kvm_events_ops ioport_events = {
130 .is_begin_event = ioport_event_begin,
131 .is_end_event = ioport_event_end,
132 .decode_key = ioport_event_decode_key,
133 .name = "IO Port Access"
136 const char *kvm_events_tp[] = {
137 "kvm:kvm_entry",
138 "kvm:kvm_exit",
139 "kvm:kvm_mmio",
140 "kvm:kvm_pio",
141 NULL,
144 struct kvm_reg_events_ops kvm_reg_events_ops[] = {
145 { .name = "vmexit", .ops = &exit_events },
146 { .name = "mmio", .ops = &mmio_events },
147 { .name = "ioport", .ops = &ioport_events },
148 { NULL, NULL },
151 const char * const kvm_skip_events[] = {
152 "HLT",
153 NULL,
156 int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid)
158 if (strstr(cpuid, "Intel")) {
159 kvm->exit_reasons = vmx_exit_reasons;
160 kvm->exit_reasons_isa = "VMX";
161 } else if (strstr(cpuid, "AMD") || strstr(cpuid, "Hygon")) {
162 kvm->exit_reasons = svm_exit_reasons;
163 kvm->exit_reasons_isa = "SVM";
164 } else
165 return -ENOTSUP;
167 return 0;