qemu-log: Improve the "exec" TB execution logging
[qemu/ar7.git] / tests / ivshmem-test.c
blobe184c67a1dff94c3e282458d151868354d81f1c8
1 /*
2 * QTest testcase for ivshmem
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 * Copyright (c) 2015 Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include <glib.h>
13 #include <glib/gstdio.h>
14 #include <sys/mman.h>
15 #include "contrib/ivshmem-server/ivshmem-server.h"
16 #include "libqos/pci-pc.h"
17 #include "libqtest.h"
18 #include "qemu-common.h"
20 #define TMPSHMSIZE (1 << 20)
21 static char *tmpshm;
22 static void *tmpshmem;
23 static char *tmpdir;
24 static char *tmpserver;
26 static void save_fn(QPCIDevice *dev, int devfn, void *data)
28 QPCIDevice **pdev = (QPCIDevice **) data;
30 *pdev = dev;
33 static QPCIDevice *get_device(QPCIBus *pcibus)
35 QPCIDevice *dev;
37 dev = NULL;
38 qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
39 g_assert(dev != NULL);
41 return dev;
44 typedef struct _IVState {
45 QTestState *qtest;
46 void *reg_base, *mem_base;
47 QPCIBus *pcibus;
48 QPCIDevice *dev;
49 } IVState;
51 enum Reg {
52 INTRMASK = 0,
53 INTRSTATUS = 4,
54 IVPOSITION = 8,
55 DOORBELL = 12,
58 static const char* reg2str(enum Reg reg) {
59 switch (reg) {
60 case INTRMASK:
61 return "IntrMask";
62 case INTRSTATUS:
63 return "IntrStatus";
64 case IVPOSITION:
65 return "IVPosition";
66 case DOORBELL:
67 return "DoorBell";
68 default:
69 return NULL;
73 static inline unsigned in_reg(IVState *s, enum Reg reg)
75 const char *name = reg2str(reg);
76 QTestState *qtest = global_qtest;
77 unsigned res;
79 global_qtest = s->qtest;
80 res = qpci_io_readl(s->dev, s->reg_base + reg);
81 g_test_message("*%s -> %x\n", name, res);
82 global_qtest = qtest;
84 return res;
87 static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
89 const char *name = reg2str(reg);
90 QTestState *qtest = global_qtest;
92 global_qtest = s->qtest;
93 g_test_message("%x -> *%s\n", v, name);
94 qpci_io_writel(s->dev, s->reg_base + reg, v);
95 global_qtest = qtest;
98 static void cleanup_vm(IVState *s)
100 g_free(s->dev);
101 qpci_free_pc(s->pcibus);
102 qtest_quit(s->qtest);
105 static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
107 uint64_t barsize;
109 s->qtest = qtest_start(cmd);
110 s->pcibus = qpci_init_pc();
111 s->dev = get_device(s->pcibus);
113 /* FIXME: other bar order fails, mappings changes */
114 s->mem_base = qpci_iomap(s->dev, 2, &barsize);
115 g_assert_nonnull(s->mem_base);
116 g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
118 if (msix) {
119 qpci_msix_enable(s->dev);
122 s->reg_base = qpci_iomap(s->dev, 0, &barsize);
123 g_assert_nonnull(s->reg_base);
124 g_assert_cmpuint(barsize, ==, 256);
126 qpci_device_enable(s->dev);
129 static void setup_vm(IVState *s)
131 char *cmd = g_strdup_printf("-device ivshmem,shm=%s,size=1M", tmpshm);
133 setup_vm_cmd(s, cmd, false);
135 g_free(cmd);
138 static void test_ivshmem_single(void)
140 IVState state, *s;
141 uint32_t data[1024];
142 int i;
144 setup_vm(&state);
145 s = &state;
147 /* valid io */
148 out_reg(s, INTRMASK, 0);
149 in_reg(s, INTRSTATUS);
150 in_reg(s, IVPOSITION);
152 out_reg(s, INTRMASK, 0xffffffff);
153 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
154 out_reg(s, INTRSTATUS, 1);
155 /* XXX: intercept IRQ, not seen in resp */
156 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
158 /* invalid io */
159 out_reg(s, IVPOSITION, 1);
160 out_reg(s, DOORBELL, 8 << 16);
162 for (i = 0; i < G_N_ELEMENTS(data); i++) {
163 data[i] = i;
165 qtest_memwrite(s->qtest, (uintptr_t)s->mem_base, data, sizeof(data));
167 for (i = 0; i < G_N_ELEMENTS(data); i++) {
168 g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
171 memset(data, 0, sizeof(data));
173 qtest_memread(s->qtest, (uintptr_t)s->mem_base, data, sizeof(data));
174 for (i = 0; i < G_N_ELEMENTS(data); i++) {
175 g_assert_cmpuint(data[i], ==, i);
178 cleanup_vm(s);
181 static void test_ivshmem_pair(void)
183 IVState state1, state2, *s1, *s2;
184 char *data;
185 int i;
187 setup_vm(&state1);
188 s1 = &state1;
189 setup_vm(&state2);
190 s2 = &state2;
192 data = g_malloc0(TMPSHMSIZE);
194 /* host write, guest 1 & 2 read */
195 memset(tmpshmem, 0x42, TMPSHMSIZE);
196 qtest_memread(s1->qtest, (uintptr_t)s1->mem_base, data, TMPSHMSIZE);
197 for (i = 0; i < TMPSHMSIZE; i++) {
198 g_assert_cmpuint(data[i], ==, 0x42);
200 qtest_memread(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
201 for (i = 0; i < TMPSHMSIZE; i++) {
202 g_assert_cmpuint(data[i], ==, 0x42);
205 /* guest 1 write, guest 2 read */
206 memset(data, 0x43, TMPSHMSIZE);
207 qtest_memwrite(s1->qtest, (uintptr_t)s1->mem_base, data, TMPSHMSIZE);
208 memset(data, 0, TMPSHMSIZE);
209 qtest_memread(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
210 for (i = 0; i < TMPSHMSIZE; i++) {
211 g_assert_cmpuint(data[i], ==, 0x43);
214 /* guest 2 write, guest 1 read */
215 memset(data, 0x44, TMPSHMSIZE);
216 qtest_memwrite(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
217 memset(data, 0, TMPSHMSIZE);
218 qtest_memread(s1->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
219 for (i = 0; i < TMPSHMSIZE; i++) {
220 g_assert_cmpuint(data[i], ==, 0x44);
223 cleanup_vm(s1);
224 cleanup_vm(s2);
225 g_free(data);
228 typedef struct ServerThread {
229 GThread *thread;
230 IvshmemServer *server;
231 int pipe[2]; /* to handle quit */
232 } ServerThread;
234 static void *server_thread(void *data)
236 ServerThread *t = data;
237 IvshmemServer *server = t->server;
239 while (true) {
240 fd_set fds;
241 int maxfd, ret;
243 FD_ZERO(&fds);
244 FD_SET(t->pipe[0], &fds);
245 maxfd = t->pipe[0] + 1;
247 ivshmem_server_get_fds(server, &fds, &maxfd);
249 ret = select(maxfd, &fds, NULL, NULL, NULL);
251 if (ret < 0) {
252 if (errno == EINTR) {
253 continue;
256 g_critical("select error: %s\n", strerror(errno));
257 break;
259 if (ret == 0) {
260 continue;
263 if (FD_ISSET(t->pipe[0], &fds)) {
264 break;
267 if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
268 g_critical("ivshmem_server_handle_fds() failed\n");
269 break;
273 return NULL;
276 static void setup_vm_with_server(IVState *s, int nvectors, bool msi)
278 char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
279 "-device ivshmem,size=1M,chardev=chr0,vectors=%d,msi=%s",
280 tmpserver, nvectors, msi ? "true" : "false");
282 setup_vm_cmd(s, cmd, msi);
284 g_free(cmd);
287 static void test_ivshmem_server(bool msi)
289 IVState state1, state2, *s1, *s2;
290 ServerThread thread;
291 IvshmemServer server;
292 int ret, vm1, vm2;
293 int nvectors = 2;
294 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
296 memset(tmpshmem, 0x42, TMPSHMSIZE);
297 ret = ivshmem_server_init(&server, tmpserver, tmpshm,
298 TMPSHMSIZE, nvectors,
299 g_test_verbose());
300 g_assert_cmpint(ret, ==, 0);
302 ret = ivshmem_server_start(&server);
303 g_assert_cmpint(ret, ==, 0);
305 setup_vm_with_server(&state1, nvectors, msi);
306 s1 = &state1;
307 setup_vm_with_server(&state2, nvectors, msi);
308 s2 = &state2;
310 g_assert_cmpuint(in_reg(s1, IVPOSITION), ==, 0xffffffff);
311 g_assert_cmpuint(in_reg(s2, IVPOSITION), ==, 0xffffffff);
313 g_assert_cmpuint(qtest_readb(s1->qtest, (uintptr_t)s1->mem_base), ==, 0x00);
315 thread.server = &server;
316 ret = pipe(thread.pipe);
317 g_assert_cmpint(ret, ==, 0);
318 thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
319 g_assert(thread.thread != NULL);
321 /* waiting until mapping is done */
322 while (g_get_monotonic_time() < end_time) {
323 g_usleep(1000);
325 if (qtest_readb(s1->qtest, (uintptr_t)s1->mem_base) == 0x42 &&
326 qtest_readb(s2->qtest, (uintptr_t)s2->mem_base) == 0x42) {
327 break;
331 /* check got different VM ids */
332 vm1 = in_reg(s1, IVPOSITION);
333 vm2 = in_reg(s2, IVPOSITION);
334 g_assert_cmpuint(vm1, !=, vm2);
336 global_qtest = s1->qtest;
337 if (msi) {
338 ret = qpci_msix_table_size(s1->dev);
339 g_assert_cmpuint(ret, ==, nvectors);
342 /* ping vm2 -> vm1 */
343 if (msi) {
344 ret = qpci_msix_pending(s1->dev, 0);
345 g_assert_cmpuint(ret, ==, 0);
346 } else {
347 out_reg(s1, INTRSTATUS, 0);
349 out_reg(s2, DOORBELL, vm1 << 16);
350 do {
351 g_usleep(10000);
352 ret = msi ? qpci_msix_pending(s1->dev, 0) : in_reg(s1, INTRSTATUS);
353 } while (ret == 0 && g_get_monotonic_time() < end_time);
354 g_assert_cmpuint(ret, !=, 0);
356 /* ping vm1 -> vm2 */
357 global_qtest = s2->qtest;
358 if (msi) {
359 ret = qpci_msix_pending(s2->dev, 0);
360 g_assert_cmpuint(ret, ==, 0);
361 } else {
362 out_reg(s2, INTRSTATUS, 0);
364 out_reg(s1, DOORBELL, vm2 << 16);
365 do {
366 g_usleep(10000);
367 ret = msi ? qpci_msix_pending(s2->dev, 0) : in_reg(s2, INTRSTATUS);
368 } while (ret == 0 && g_get_monotonic_time() < end_time);
369 g_assert_cmpuint(ret, !=, 0);
371 cleanup_vm(s2);
372 cleanup_vm(s1);
374 if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
375 g_error("qemu_write_full: %s", g_strerror(errno));
378 g_thread_join(thread.thread);
380 ivshmem_server_close(&server);
381 close(thread.pipe[1]);
382 close(thread.pipe[0]);
385 static void test_ivshmem_server_msi(void)
387 test_ivshmem_server(true);
390 static void test_ivshmem_server_irq(void)
392 test_ivshmem_server(false);
395 #define PCI_SLOT_HP 0x06
397 static void test_ivshmem_hotplug(void)
399 gchar *opts;
401 qtest_start("");
403 opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
405 qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
406 qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
408 qtest_end();
409 g_free(opts);
412 static void test_ivshmem_memdev(void)
414 IVState state;
416 /* just for the sake of checking memory-backend property */
417 setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
418 " -device ivshmem,x-memdev=mb1", false);
420 cleanup_vm(&state);
423 static void cleanup(void)
425 if (tmpshmem) {
426 munmap(tmpshmem, TMPSHMSIZE);
427 tmpshmem = NULL;
430 if (tmpshm) {
431 shm_unlink(tmpshm);
432 g_free(tmpshm);
433 tmpshm = NULL;
436 if (tmpserver) {
437 g_unlink(tmpserver);
438 g_free(tmpserver);
439 tmpserver = NULL;
442 if (tmpdir) {
443 g_rmdir(tmpdir);
444 tmpdir = NULL;
448 static void abrt_handler(void *data)
450 cleanup();
453 static gchar *mktempshm(int size, int *fd)
455 while (true) {
456 gchar *name;
458 name = g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
459 *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
460 S_IRWXU|S_IRWXG|S_IRWXO);
461 if (*fd > 0) {
462 g_assert(ftruncate(*fd, size) == 0);
463 return name;
466 g_free(name);
468 if (errno != EEXIST) {
469 perror("shm_open");
470 return NULL;
475 int main(int argc, char **argv)
477 int ret, fd;
478 gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
480 #if !GLIB_CHECK_VERSION(2, 31, 0)
481 if (!g_thread_supported()) {
482 g_thread_init(NULL);
484 #endif
486 g_test_init(&argc, &argv, NULL);
488 qtest_add_abrt_handler(abrt_handler, NULL);
489 /* shm */
490 tmpshm = mktempshm(TMPSHMSIZE, &fd);
491 if (!tmpshm) {
492 return 0;
494 tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
495 g_assert(tmpshmem != MAP_FAILED);
496 /* server */
497 if (mkdtemp(dir) == NULL) {
498 g_error("mkdtemp: %s", g_strerror(errno));
500 tmpdir = dir;
501 tmpserver = g_strconcat(tmpdir, "/server", NULL);
503 qtest_add_func("/ivshmem/single", test_ivshmem_single);
504 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
505 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
506 if (g_test_slow()) {
507 qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
508 qtest_add_func("/ivshmem/server-msi", test_ivshmem_server_msi);
509 qtest_add_func("/ivshmem/server-irq", test_ivshmem_server_irq);
512 ret = g_test_run();
514 cleanup();
516 return ret;