master
c 380 lines 12.6 KB
Raw
1 /*
2 * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
3 *
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7 #include <inttypes.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <glib.h>
14
15 #include <stdbool.h>
16 #include <qemu-plugin.h>
17
18 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
19
20 typedef struct {
21 uint64_t mem_count;
22 uint64_t io_count;
23 } CPUCount;
24
25 typedef struct {
26 uint64_t vaddr;
27 const char *sym;
28 } InsnInfo;
29
30 /*
31 * For the "memory" system test we need to track accesses to
32 * individual regions. We mirror the data written to the region and
33 * then check when it is read that it matches up.
34 *
35 * We do this as regions rather than pages to save on complications
36 * with page crossing and the fact the test only cares about the
37 * test_data region.
38 */
39 static uint64_t region_size = 4096 * 4;
40 static uint64_t region_mask;
41
42 typedef struct {
43 uint64_t region_address;
44 uint64_t reads;
45 uint64_t writes;
46 uint8_t *data;
47 /* Did we see every write and read with correct values? */
48 bool seen_all;
49 } RegionInfo;
50
51 static struct qemu_plugin_scoreboard *counts;
52 static qemu_plugin_u64 mem_count;
53 static qemu_plugin_u64 io_count;
54 static bool do_inline, do_callback, do_print_accesses, do_region_summary;
55 static bool do_haddr;
56 static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
57
58
59 static GMutex lock;
60 static GHashTable *regions;
61
62 static gint addr_order(gconstpointer a, gconstpointer b, gpointer d)
63 {
64 RegionInfo *na = (RegionInfo *) a;
65 RegionInfo *nb = (RegionInfo *) b;
66
67 return na->region_address > nb->region_address ? 1 : -1;
68 }
69
70
71 static void plugin_exit(void *p)
72 {
73 g_autoptr(GString) out = g_string_new("");
74
75 if (do_inline || do_callback) {
76 g_string_printf(out, "mem accesses: %" PRIu64 "\n",
77 qemu_plugin_u64_sum(mem_count));
78 }
79 if (do_haddr) {
80 g_string_append_printf(out, "io accesses: %" PRIu64 "\n",
81 qemu_plugin_u64_sum(io_count));
82 }
83 qemu_plugin_outs(out->str);
84
85
86 if (do_region_summary) {
87 g_autoptr(GList) regionlist = g_hash_table_get_values(regions);
88
89 regionlist = g_list_sort_with_data(regionlist, addr_order, NULL);
90
91 g_string_printf(out, "Region Base, Reads, Writes, Seen all\n");
92
93 for (GList *l = regionlist; l; l = g_list_next(l)) {
94 RegionInfo *ri = (RegionInfo *) l->data;
95
96 g_string_append_printf(out,
97 "0x%016"PRIx64", "
98 "%"PRId64", %"PRId64", %s\n",
99 ri->region_address,
100 ri->reads,
101 ri->writes,
102 ri->seen_all ? "true" : "false");
103 }
104 qemu_plugin_outs(out->str);
105 }
106
107 qemu_plugin_scoreboard_free(counts);
108 }
109
110 /*
111 * Update the region tracking info for the access. We split up accesses
112 * that span regions even though the plugin infrastructure will deliver
113 * it as a single access.
114 */
115 static void update_region_info(uint64_t region, uint64_t offset,
116 qemu_plugin_meminfo_t meminfo,
117 qemu_plugin_mem_value value,
118 unsigned size)
119 {
120 bool be = qemu_plugin_mem_is_big_endian(meminfo);
121 bool is_store = qemu_plugin_mem_is_store(meminfo);
122 RegionInfo *ri;
123 bool unseen_data = false;
124 void *val_ptr;
125 unsigned int val_size;
126 qemu_plugin_mem_value swapped_value;
127
128 g_assert(offset + size <= region_size);
129
130 g_mutex_lock(&lock);
131 ri = (RegionInfo *) g_hash_table_lookup(regions, &region);
132
133 if (!ri) {
134 ri = g_new0(RegionInfo, 1);
135 ri->region_address = region;
136 ri->data = g_malloc0(region_size);
137 ri->seen_all = true;
138 g_hash_table_insert(regions, &ri->region_address, ri);
139 }
140
141 if (is_store) {
142 ri->writes++;
143 } else {
144 ri->reads++;
145 }
146
147 void *ri_data = &ri->data[offset];
148
149 swapped_value.type = value.type;
150 switch (value.type) {
151 case QEMU_PLUGIN_MEM_VALUE_U8:
152 swapped_value.data.u8 = value.data.u8;
153 val_ptr = &swapped_value.data.u8;
154 val_size = 1;
155 break;
156 case QEMU_PLUGIN_MEM_VALUE_U16:
157 swapped_value.data.u16 = be ? GUINT16_TO_BE(value.data.u16) :
158 GUINT16_TO_LE(value.data.u16);
159 val_ptr = &swapped_value.data.u16;
160 val_size = 2;
161 break;
162 case QEMU_PLUGIN_MEM_VALUE_U32:
163 swapped_value.data.u32 = be ? GUINT32_TO_BE(value.data.u32) :
164 GUINT32_TO_LE(value.data.u32);
165 val_ptr = &swapped_value.data.u32;
166 val_size = 4;
167 break;
168 case QEMU_PLUGIN_MEM_VALUE_U64:
169 swapped_value.data.u64 = be ? GUINT64_TO_BE(value.data.u64) :
170 GUINT64_TO_LE(value.data.u64);
171 val_ptr = &swapped_value.data.u64;
172 val_size = 8;
173 break;
174 case QEMU_PLUGIN_MEM_VALUE_U128:
175 /* none in test so skip */
176 goto done;
177 default:
178 g_assert_not_reached();
179 }
180
181 /* ri_data may not be aligned, so we use memcpy/memcmp */
182 if (is_store) {
183 memcpy(ri_data, val_ptr, val_size);
184 } else {
185 unseen_data = memcmp(ri_data, val_ptr, val_size) != 0;
186 }
187
188 /*
189 * This is expected for regions initialised by QEMU (.text etc) but we
190 * expect to see all data read and written to the test_data region
191 * of the memory test.
192 */
193 if (unseen_data && ri->seen_all) {
194 g_autoptr(GString) error = g_string_new("Warning: ");
195 g_string_append_printf(error, "0x%016"PRIx64":%"PRId64
196 " read an un-instrumented value\n",
197 region, offset);
198 qemu_plugin_outs(error->str);
199 ri->seen_all = false;
200 }
201
202 done:
203 g_mutex_unlock(&lock);
204 }
205
206 static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
207 uint64_t vaddr, void *udata)
208 {
209 if (do_haddr) {
210 struct qemu_plugin_hwaddr *hwaddr;
211 hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr);
212 if (qemu_plugin_hwaddr_is_io(hwaddr)) {
213 qemu_plugin_u64_add(io_count, cpu_index, 1);
214 } else {
215 qemu_plugin_u64_add(mem_count, cpu_index, 1);
216 }
217 } else {
218 qemu_plugin_u64_add(mem_count, cpu_index, 1);
219 }
220
221 if (do_region_summary) {
222 uint64_t region = vaddr & ~region_mask;
223 uint64_t offset = vaddr & region_mask;
224 qemu_plugin_mem_value value = qemu_plugin_mem_get_value(meminfo);
225 unsigned size = 1 << qemu_plugin_mem_size_shift(meminfo);
226
227 update_region_info(region, offset, meminfo, value, size);
228 }
229 }
230
231 static void print_access(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
232 uint64_t vaddr, void *udata)
233 {
234 InsnInfo *insn_info = udata;
235 unsigned size = 8 << qemu_plugin_mem_size_shift(meminfo);
236 const char *type = qemu_plugin_mem_is_store(meminfo) ? "store" : "load";
237 qemu_plugin_mem_value value = qemu_plugin_mem_get_value(meminfo);
238 uint64_t hwaddr =
239 qemu_plugin_hwaddr_phys_addr(qemu_plugin_get_hwaddr(meminfo, vaddr));
240 g_autoptr(GString) out = g_string_new("");
241 g_string_printf(out,
242 "0x%"PRIx64",%s,0x%"PRIx64",0x%"PRIx64",%d,%s,",
243 insn_info->vaddr, insn_info->sym,
244 vaddr, hwaddr, size, type);
245 switch (value.type) {
246 case QEMU_PLUGIN_MEM_VALUE_U8:
247 g_string_append_printf(out, "0x%02"PRIx8, value.data.u8);
248 break;
249 case QEMU_PLUGIN_MEM_VALUE_U16:
250 g_string_append_printf(out, "0x%04"PRIx16, value.data.u16);
251 break;
252 case QEMU_PLUGIN_MEM_VALUE_U32:
253 g_string_append_printf(out, "0x%08"PRIx32, value.data.u32);
254 break;
255 case QEMU_PLUGIN_MEM_VALUE_U64:
256 g_string_append_printf(out, "0x%016"PRIx64, value.data.u64);
257 break;
258 case QEMU_PLUGIN_MEM_VALUE_U128:
259 g_string_append_printf(out, "0x%016"PRIx64"%016"PRIx64,
260 value.data.u128.high, value.data.u128.low);
261 break;
262 default:
263 g_assert_not_reached();
264 }
265 g_string_append_printf(out, "\n");
266 qemu_plugin_outs(out->str);
267 }
268
269 static void vcpu_tb_trans(struct qemu_plugin_tb *tb, void *userdata)
270 {
271 size_t n = qemu_plugin_tb_n_insns(tb);
272 size_t i;
273
274 for (i = 0; i < n; i++) {
275 struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
276
277 if (do_inline) {
278 qemu_plugin_register_vcpu_mem_inline_per_vcpu(
279 insn, rw,
280 QEMU_PLUGIN_INLINE_ADD_U64,
281 mem_count, 1);
282 }
283 if (do_callback || do_region_summary) {
284 qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
285 QEMU_PLUGIN_CB_NO_REGS,
286 rw, NULL);
287 }
288 if (do_print_accesses) {
289 /* we leak this pointer, to avoid locking to keep track of it */
290 InsnInfo *insn_info = g_malloc(sizeof(InsnInfo));
291 const char *sym = qemu_plugin_insn_symbol(insn);
292 insn_info->sym = sym ? sym : "";
293 insn_info->vaddr = qemu_plugin_insn_vaddr(insn);
294 qemu_plugin_register_vcpu_mem_cb(insn, print_access,
295 QEMU_PLUGIN_CB_NO_REGS,
296 rw, (void *) insn_info);
297 }
298 }
299 }
300
301 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
302 const qemu_info_t *info,
303 int argc, char **argv)
304 {
305
306 for (int i = 0; i < argc; i++) {
307 char *opt = argv[i];
308 g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
309
310 if (g_strcmp0(tokens[0], "haddr") == 0) {
311 if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_haddr)) {
312 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
313 return -1;
314 }
315 } else if (g_strcmp0(tokens[0], "track") == 0) {
316 if (g_strcmp0(tokens[1], "r") == 0) {
317 rw = QEMU_PLUGIN_MEM_R;
318 } else if (g_strcmp0(tokens[1], "w") == 0) {
319 rw = QEMU_PLUGIN_MEM_W;
320 } else if (g_strcmp0(tokens[1], "rw") == 0) {
321 rw = QEMU_PLUGIN_MEM_RW;
322 } else {
323 fprintf(stderr, "invalid value for argument track: %s\n", opt);
324 return -1;
325 }
326 } else if (g_strcmp0(tokens[0], "inline") == 0) {
327 if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
328 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
329 return -1;
330 }
331 } else if (g_strcmp0(tokens[0], "callback") == 0) {
332 if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_callback)) {
333 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
334 return -1;
335 }
336 } else if (g_strcmp0(tokens[0], "print-accesses") == 0) {
337 if (!qemu_plugin_bool_parse(tokens[0], tokens[1],
338 &do_print_accesses)) {
339 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
340 return -1;
341 }
342 } else if (g_strcmp0(tokens[0], "region-summary") == 0) {
343 if (!qemu_plugin_bool_parse(tokens[0], tokens[1],
344 &do_region_summary)) {
345 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
346 return -1;
347 }
348 } else {
349 fprintf(stderr, "option parsing failed: %s\n", opt);
350 return -1;
351 }
352 }
353
354 if (do_inline && do_callback) {
355 fprintf(stderr,
356 "can't enable inline and callback counting at the same time\n");
357 return -1;
358 }
359
360 if (do_print_accesses) {
361 g_autoptr(GString) out = g_string_new("");
362 g_string_printf(out,
363 "insn_vaddr,insn_symbol,mem_vaddr,mem_hwaddr,"
364 "access_size,access_type,mem_value\n");
365 qemu_plugin_outs(out->str);
366 }
367
368 if (do_region_summary) {
369 region_mask = (region_size - 1);
370 regions = g_hash_table_new(g_int64_hash, g_int64_equal);
371 }
372
373 counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
374 mem_count = qemu_plugin_scoreboard_u64_in_struct(
375 counts, CPUCount, mem_count);
376 io_count = qemu_plugin_scoreboard_u64_in_struct(counts, CPUCount, io_count);
377 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans, NULL);
378 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
379 return 0;
380 }