xref: /openbmc/linux/tools/perf/arch/x86/util/kvm-stat.c (revision e7bae9bb)
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>
9 
10 define_exit_reasons_table(vmx_exit_reasons, VMX_EXIT_REASONS);
11 define_exit_reasons_table(svm_exit_reasons, SVM_EXIT_REASONS);
12 
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"
18 };
19 
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";
25 
26 /*
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...).
30  */
31 static void mmio_event_get_key(struct evsel *evsel, struct perf_sample *sample,
32 			       struct event_key *key)
33 {
34 	key->key  = evsel__intval(evsel, sample, "gpa");
35 	key->info = evsel__intval(evsel, sample, "type");
36 }
37 
38 #define KVM_TRACE_MMIO_READ_UNSATISFIED 0
39 #define KVM_TRACE_MMIO_READ 1
40 #define KVM_TRACE_MMIO_WRITE 2
41 
42 static bool mmio_event_begin(struct evsel *evsel,
43 			     struct perf_sample *sample, struct event_key *key)
44 {
45 	/* MMIO read begin event in kernel. */
46 	if (kvm_exit_event(evsel))
47 		return true;
48 
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;
54 	}
55 
56 	return false;
57 }
58 
59 static bool mmio_event_end(struct evsel *evsel, struct perf_sample *sample,
60 			   struct event_key *key)
61 {
62 	/* MMIO write end event in kernel. */
63 	if (kvm_entry_event(evsel))
64 		return true;
65 
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;
71 	}
72 
73 	return false;
74 }
75 
76 static void mmio_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
77 				  struct event_key *key,
78 				  char *decode)
79 {
80 	scnprintf(decode, decode_str_len, "%#lx:%s",
81 		  (unsigned long)key->key,
82 		  key->info == KVM_TRACE_MMIO_WRITE ? "W" : "R");
83 }
84 
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"
90 };
91 
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)
96 {
97 	key->key  = evsel__intval(evsel, sample, "port");
98 	key->info = evsel__intval(evsel, sample, "rw");
99 }
100 
101 static bool ioport_event_begin(struct evsel *evsel,
102 			       struct perf_sample *sample,
103 			       struct event_key *key)
104 {
105 	if (!strcmp(evsel->name, "kvm:kvm_pio")) {
106 		ioport_event_get_key(evsel, sample, key);
107 		return true;
108 	}
109 
110 	return false;
111 }
112 
113 static bool ioport_event_end(struct evsel *evsel,
114 			     struct perf_sample *sample __maybe_unused,
115 			     struct event_key *key __maybe_unused)
116 {
117 	return kvm_entry_event(evsel);
118 }
119 
120 static void ioport_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
121 				    struct event_key *key,
122 				    char *decode)
123 {
124 	scnprintf(decode, decode_str_len, "%#llx:%s",
125 		  (unsigned long long)key->key,
126 		  key->info ? "POUT" : "PIN");
127 }
128 
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"
134 };
135 
136 const char *kvm_events_tp[] = {
137 	"kvm:kvm_entry",
138 	"kvm:kvm_exit",
139 	"kvm:kvm_mmio",
140 	"kvm:kvm_pio",
141 	NULL,
142 };
143 
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 },
149 };
150 
151 const char * const kvm_skip_events[] = {
152 	"HLT",
153 	NULL,
154 };
155 
156 int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid)
157 {
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;
166 
167 	return 0;
168 }
169