Rename mis->file to from_src_file
[qemu/ar7.git] / tests / ivshmem-test.c
blobc8f0cf0f70d11e3d1a665667c37defb7fd5cfac8
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 <errno.h>
12 #include <fcntl.h>
13 #include <glib.h>
14 #include <glib/gstdio.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <unistd.h>
18 #include "contrib/ivshmem-server/ivshmem-server.h"
19 #include "libqos/pci-pc.h"
20 #include "libqtest.h"
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
24 #define TMPSHMSIZE (1 << 20)
25 static char *tmpshm;
26 static void *tmpshmem;
27 static char *tmpdir;
28 static char *tmpserver;
30 static void save_fn(QPCIDevice *dev, int devfn, void *data)
32 QPCIDevice **pdev = (QPCIDevice **) data;
34 *pdev = dev;
37 static QPCIDevice *get_device(void)
39 QPCIDevice *dev;
40 QPCIBus *pcibus;
42 pcibus = qpci_init_pc();
43 qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
44 g_assert(dev != NULL);
46 return dev;
49 typedef struct _IVState {
50 QTestState *qtest;
51 void *reg_base, *mem_base;
52 QPCIDevice *dev;
53 } IVState;
55 enum Reg {
56 INTRMASK = 0,
57 INTRSTATUS = 4,
58 IVPOSITION = 8,
59 DOORBELL = 12,
62 static const char* reg2str(enum Reg reg) {
63 switch (reg) {
64 case INTRMASK:
65 return "IntrMask";
66 case INTRSTATUS:
67 return "IntrStatus";
68 case IVPOSITION:
69 return "IVPosition";
70 case DOORBELL:
71 return "DoorBell";
72 default:
73 return NULL;
77 static inline unsigned in_reg(IVState *s, enum Reg reg)
79 const char *name = reg2str(reg);
80 QTestState *qtest = global_qtest;
81 unsigned res;
83 global_qtest = s->qtest;
84 res = qpci_io_readl(s->dev, s->reg_base + reg);
85 g_test_message("*%s -> %x\n", name, res);
86 global_qtest = qtest;
88 return res;
91 static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
93 const char *name = reg2str(reg);
94 QTestState *qtest = global_qtest;
96 global_qtest = s->qtest;
97 g_test_message("%x -> *%s\n", v, name);
98 qpci_io_writel(s->dev, s->reg_base + reg, v);
99 global_qtest = qtest;
102 static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
104 uint64_t barsize;
106 s->qtest = qtest_start(cmd);
108 s->dev = get_device();
110 /* FIXME: other bar order fails, mappings changes */
111 s->mem_base = qpci_iomap(s->dev, 2, &barsize);
112 g_assert_nonnull(s->mem_base);
113 g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
115 if (msix) {
116 qpci_msix_enable(s->dev);
119 s->reg_base = qpci_iomap(s->dev, 0, &barsize);
120 g_assert_nonnull(s->reg_base);
121 g_assert_cmpuint(barsize, ==, 256);
123 qpci_device_enable(s->dev);
126 static void setup_vm(IVState *s)
128 char *cmd = g_strdup_printf("-device ivshmem,shm=%s,size=1M", tmpshm);
130 setup_vm_cmd(s, cmd, false);
132 g_free(cmd);
135 static void test_ivshmem_single(void)
137 IVState state, *s;
138 uint32_t data[1024];
139 int i;
141 setup_vm(&state);
142 s = &state;
144 /* valid io */
145 out_reg(s, INTRMASK, 0);
146 in_reg(s, INTRSTATUS);
147 in_reg(s, IVPOSITION);
149 out_reg(s, INTRMASK, 0xffffffff);
150 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
151 out_reg(s, INTRSTATUS, 1);
152 /* XXX: intercept IRQ, not seen in resp */
153 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
155 /* invalid io */
156 out_reg(s, IVPOSITION, 1);
157 out_reg(s, DOORBELL, 8 << 16);
159 for (i = 0; i < G_N_ELEMENTS(data); i++) {
160 data[i] = i;
162 qtest_memwrite(s->qtest, (uintptr_t)s->mem_base, data, sizeof(data));
164 for (i = 0; i < G_N_ELEMENTS(data); i++) {
165 g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
168 memset(data, 0, sizeof(data));
170 qtest_memread(s->qtest, (uintptr_t)s->mem_base, data, sizeof(data));
171 for (i = 0; i < G_N_ELEMENTS(data); i++) {
172 g_assert_cmpuint(data[i], ==, i);
175 qtest_quit(s->qtest);
178 static void test_ivshmem_pair(void)
180 IVState state1, state2, *s1, *s2;
181 char *data;
182 int i;
184 setup_vm(&state1);
185 s1 = &state1;
186 setup_vm(&state2);
187 s2 = &state2;
189 data = g_malloc0(TMPSHMSIZE);
191 /* host write, guest 1 & 2 read */
192 memset(tmpshmem, 0x42, TMPSHMSIZE);
193 qtest_memread(s1->qtest, (uintptr_t)s1->mem_base, data, TMPSHMSIZE);
194 for (i = 0; i < TMPSHMSIZE; i++) {
195 g_assert_cmpuint(data[i], ==, 0x42);
197 qtest_memread(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
198 for (i = 0; i < TMPSHMSIZE; i++) {
199 g_assert_cmpuint(data[i], ==, 0x42);
202 /* guest 1 write, guest 2 read */
203 memset(data, 0x43, TMPSHMSIZE);
204 qtest_memwrite(s1->qtest, (uintptr_t)s1->mem_base, data, TMPSHMSIZE);
205 memset(data, 0, TMPSHMSIZE);
206 qtest_memread(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
207 for (i = 0; i < TMPSHMSIZE; i++) {
208 g_assert_cmpuint(data[i], ==, 0x43);
211 /* guest 2 write, guest 1 read */
212 memset(data, 0x44, TMPSHMSIZE);
213 qtest_memwrite(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
214 memset(data, 0, TMPSHMSIZE);
215 qtest_memread(s1->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
216 for (i = 0; i < TMPSHMSIZE; i++) {
217 g_assert_cmpuint(data[i], ==, 0x44);
220 qtest_quit(s1->qtest);
221 qtest_quit(s2->qtest);
222 g_free(data);
225 typedef struct ServerThread {
226 GThread *thread;
227 IvshmemServer *server;
228 int pipe[2]; /* to handle quit */
229 } ServerThread;
231 static void *server_thread(void *data)
233 ServerThread *t = data;
234 IvshmemServer *server = t->server;
236 while (true) {
237 fd_set fds;
238 int maxfd, ret;
240 FD_ZERO(&fds);
241 FD_SET(t->pipe[0], &fds);
242 maxfd = t->pipe[0] + 1;
244 ivshmem_server_get_fds(server, &fds, &maxfd);
246 ret = select(maxfd, &fds, NULL, NULL, NULL);
248 if (ret < 0) {
249 if (errno == EINTR) {
250 continue;
253 g_critical("select error: %s\n", strerror(errno));
254 break;
256 if (ret == 0) {
257 continue;
260 if (FD_ISSET(t->pipe[0], &fds)) {
261 break;
264 if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
265 g_critical("ivshmem_server_handle_fds() failed\n");
266 break;
270 return NULL;
273 static void setup_vm_with_server(IVState *s, int nvectors)
275 char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
276 "-device ivshmem,size=1M,chardev=chr0,vectors=%d",
277 tmpserver, nvectors);
279 setup_vm_cmd(s, cmd, true);
281 g_free(cmd);
284 static void test_ivshmem_server(void)
286 IVState state1, state2, *s1, *s2;
287 ServerThread thread;
288 IvshmemServer server;
289 int ret, vm1, vm2;
290 int nvectors = 2;
291 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
293 memset(tmpshmem, 0x42, TMPSHMSIZE);
294 ret = ivshmem_server_init(&server, tmpserver, tmpshm,
295 TMPSHMSIZE, nvectors,
296 g_test_verbose());
297 g_assert_cmpint(ret, ==, 0);
299 ret = ivshmem_server_start(&server);
300 g_assert_cmpint(ret, ==, 0);
302 setup_vm_with_server(&state1, nvectors);
303 s1 = &state1;
304 setup_vm_with_server(&state2, nvectors);
305 s2 = &state2;
307 g_assert_cmpuint(in_reg(s1, IVPOSITION), ==, 0xffffffff);
308 g_assert_cmpuint(in_reg(s2, IVPOSITION), ==, 0xffffffff);
310 g_assert_cmpuint(qtest_readb(s1->qtest, (uintptr_t)s1->mem_base), ==, 0x00);
312 thread.server = &server;
313 ret = pipe(thread.pipe);
314 g_assert_cmpint(ret, ==, 0);
315 thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
316 g_assert(thread.thread != NULL);
318 /* waiting until mapping is done */
319 while (g_get_monotonic_time() < end_time) {
320 g_usleep(1000);
322 if (qtest_readb(s1->qtest, (uintptr_t)s1->mem_base) == 0x42 &&
323 qtest_readb(s2->qtest, (uintptr_t)s2->mem_base) == 0x42) {
324 break;
328 /* check got different VM ids */
329 vm1 = in_reg(s1, IVPOSITION);
330 vm2 = in_reg(s2, IVPOSITION);
331 g_assert_cmpuint(vm1, !=, vm2);
333 global_qtest = s1->qtest;
334 ret = qpci_msix_table_size(s1->dev);
335 g_assert_cmpuint(ret, ==, nvectors);
337 /* ping vm2 -> vm1 */
338 ret = qpci_msix_pending(s1->dev, 0);
339 g_assert_cmpuint(ret, ==, 0);
340 out_reg(s2, DOORBELL, vm1 << 16);
341 do {
342 g_usleep(10000);
343 ret = qpci_msix_pending(s1->dev, 0);
344 } while (ret == 0 && g_get_monotonic_time() < end_time);
345 g_assert_cmpuint(ret, !=, 0);
347 /* ping vm1 -> vm2 */
348 global_qtest = s2->qtest;
349 ret = qpci_msix_pending(s2->dev, 0);
350 g_assert_cmpuint(ret, ==, 0);
351 out_reg(s1, DOORBELL, vm2 << 16);
352 do {
353 g_usleep(10000);
354 ret = qpci_msix_pending(s2->dev, 0);
355 } while (ret == 0 && g_get_monotonic_time() < end_time);
356 g_assert_cmpuint(ret, !=, 0);
358 qtest_quit(s2->qtest);
359 qtest_quit(s1->qtest);
361 if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
362 g_error("qemu_write_full: %s", g_strerror(errno));
365 g_thread_join(thread.thread);
367 ivshmem_server_close(&server);
368 close(thread.pipe[1]);
369 close(thread.pipe[0]);
372 #define PCI_SLOT_HP 0x06
374 static void test_ivshmem_hotplug(void)
376 gchar *opts;
378 qtest_start("");
380 opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
382 qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
383 qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
385 qtest_end();
386 g_free(opts);
389 static void test_ivshmem_memdev(void)
391 IVState state;
393 /* just for the sake of checking memory-backend property */
394 setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
395 " -device ivshmem,memdev=mb1", false);
397 qtest_quit(state.qtest);
400 static void cleanup(void)
402 if (tmpshmem) {
403 munmap(tmpshmem, TMPSHMSIZE);
404 tmpshmem = NULL;
407 if (tmpshm) {
408 shm_unlink(tmpshm);
409 g_free(tmpshm);
410 tmpshm = NULL;
413 if (tmpserver) {
414 g_unlink(tmpserver);
415 g_free(tmpserver);
416 tmpserver = NULL;
419 if (tmpdir) {
420 g_rmdir(tmpdir);
421 tmpdir = NULL;
425 static void abrt_handler(void *data)
427 cleanup();
430 static gchar *mktempshm(int size, int *fd)
432 while (true) {
433 gchar *name;
435 name = g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
436 *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
437 S_IRWXU|S_IRWXG|S_IRWXO);
438 if (*fd > 0) {
439 g_assert(ftruncate(*fd, size) == 0);
440 return name;
443 g_free(name);
445 if (errno != EEXIST) {
446 perror("shm_open");
447 return NULL;
452 int main(int argc, char **argv)
454 int ret, fd;
455 gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
457 #if !GLIB_CHECK_VERSION(2, 31, 0)
458 if (!g_thread_supported()) {
459 g_thread_init(NULL);
461 #endif
463 g_test_init(&argc, &argv, NULL);
465 qtest_add_abrt_handler(abrt_handler, NULL);
466 /* shm */
467 tmpshm = mktempshm(TMPSHMSIZE, &fd);
468 if (!tmpshm) {
469 return 0;
471 tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
472 g_assert(tmpshmem != MAP_FAILED);
473 /* server */
474 if (mkdtemp(dir) == NULL) {
475 g_error("mkdtemp: %s", g_strerror(errno));
477 tmpdir = dir;
478 tmpserver = g_strconcat(tmpdir, "/server", NULL);
480 qtest_add_func("/ivshmem/single", test_ivshmem_single);
481 qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
482 qtest_add_func("/ivshmem/server", test_ivshmem_server);
483 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
484 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
486 ret = g_test_run();
488 cleanup();
490 return ret;