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.
11 #include "qemu/osdep.h"
12 #include <glib/gstdio.h>
13 #include "contrib/ivshmem-server/ivshmem-server.h"
14 #include "libqos/libqos-pc.h"
15 #include "libqos/libqos-spapr.h"
17 #include "qemu-common.h"
19 #define TMPSHMSIZE (1 << 20)
21 static void *tmpshmem;
23 static char *tmpserver;
25 static void save_fn(QPCIDevice *dev, int devfn, void *data)
27 QPCIDevice **pdev = (QPCIDevice **) data;
32 static QPCIDevice *get_device(QPCIBus *pcibus)
37 qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
38 g_assert(dev != NULL);
43 typedef struct _IVState {
45 QPCIBar reg_bar, mem_bar;
56 static const char* reg2str(enum Reg reg) {
71 static inline unsigned in_reg(IVState *s, enum Reg reg)
73 const char *name = reg2str(reg);
76 res = qpci_io_readl(s->dev, s->reg_bar, reg);
77 g_test_message("*%s -> %x\n", name, res);
82 static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
84 const char *name = reg2str(reg);
86 g_test_message("%x -> *%s\n", v, name);
87 qpci_io_writel(s->dev, s->reg_bar, reg, v);
90 static inline void read_mem(IVState *s, uint64_t off, void *buf, size_t len)
92 qpci_memread(s->dev, s->mem_bar, off, buf, len);
95 static inline void write_mem(IVState *s, uint64_t off,
96 const void *buf, size_t len)
98 qpci_memwrite(s->dev, s->mem_bar, off, buf, len);
101 static void cleanup_vm(IVState *s)
103 assert(!global_qtest);
105 qtest_shutdown(s->qs);
108 static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
111 const char *arch = qtest_get_arch();
113 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
114 s->qs = qtest_pc_boot(cmd);
115 } else if (strcmp(arch, "ppc64") == 0) {
116 s->qs = qtest_spapr_boot(cmd);
118 g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
121 s->dev = get_device(s->qs->pcibus);
123 s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
124 g_assert_cmpuint(barsize, ==, 256);
127 qpci_msix_enable(s->dev);
130 s->mem_bar = qpci_iomap(s->dev, 2, &barsize);
131 g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
133 qpci_device_enable(s->dev);
136 static void setup_vm(IVState *s)
138 char *cmd = g_strdup_printf("-object memory-backend-file"
139 ",id=mb1,size=1M,share,mem-path=/dev/shm%s"
140 " -device ivshmem-plain,memdev=mb1", tmpshm);
142 setup_vm_cmd(s, cmd, false);
147 static void test_ivshmem_single(void)
156 /* initial state of readable registers */
157 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0);
158 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
159 g_assert_cmpuint(in_reg(s, IVPOSITION), ==, 0);
161 /* trigger interrupt via registers */
162 out_reg(s, INTRMASK, 0xffffffff);
163 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
164 out_reg(s, INTRSTATUS, 1);
165 /* check interrupt status */
166 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
168 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
169 /* TODO intercept actual interrupt (needs qtest work) */
171 /* invalid register access */
172 out_reg(s, IVPOSITION, 1);
175 /* ring the (non-functional) doorbell */
176 out_reg(s, DOORBELL, 8 << 16);
178 /* write shared memory */
179 for (i = 0; i < G_N_ELEMENTS(data); i++) {
182 write_mem(s, 0, data, sizeof(data));
185 for (i = 0; i < G_N_ELEMENTS(data); i++) {
186 g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
189 /* read it back and verify read */
190 memset(data, 0, sizeof(data));
191 read_mem(s, 0, data, sizeof(data));
192 for (i = 0; i < G_N_ELEMENTS(data); i++) {
193 g_assert_cmpuint(data[i], ==, i);
199 static void test_ivshmem_pair(void)
201 IVState state1, state2, *s1, *s2;
210 data = g_malloc0(TMPSHMSIZE);
212 /* host write, guest 1 & 2 read */
213 memset(tmpshmem, 0x42, TMPSHMSIZE);
214 read_mem(s1, 0, data, TMPSHMSIZE);
215 for (i = 0; i < TMPSHMSIZE; i++) {
216 g_assert_cmpuint(data[i], ==, 0x42);
218 read_mem(s2, 0, data, TMPSHMSIZE);
219 for (i = 0; i < TMPSHMSIZE; i++) {
220 g_assert_cmpuint(data[i], ==, 0x42);
223 /* guest 1 write, guest 2 read */
224 memset(data, 0x43, TMPSHMSIZE);
225 write_mem(s1, 0, data, TMPSHMSIZE);
226 memset(data, 0, TMPSHMSIZE);
227 read_mem(s2, 0, data, TMPSHMSIZE);
228 for (i = 0; i < TMPSHMSIZE; i++) {
229 g_assert_cmpuint(data[i], ==, 0x43);
232 /* guest 2 write, guest 1 read */
233 memset(data, 0x44, TMPSHMSIZE);
234 write_mem(s2, 0, data, TMPSHMSIZE);
235 memset(data, 0, TMPSHMSIZE);
236 read_mem(s1, 0, data, TMPSHMSIZE);
237 for (i = 0; i < TMPSHMSIZE; i++) {
238 g_assert_cmpuint(data[i], ==, 0x44);
246 typedef struct ServerThread {
248 IvshmemServer *server;
249 int pipe[2]; /* to handle quit */
252 static void *server_thread(void *data)
254 ServerThread *t = data;
255 IvshmemServer *server = t->server;
262 FD_SET(t->pipe[0], &fds);
263 maxfd = t->pipe[0] + 1;
265 ivshmem_server_get_fds(server, &fds, &maxfd);
267 ret = select(maxfd, &fds, NULL, NULL, NULL);
270 if (errno == EINTR) {
274 g_critical("select error: %s\n", strerror(errno));
281 if (FD_ISSET(t->pipe[0], &fds)) {
285 if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
286 g_critical("ivshmem_server_handle_fds() failed\n");
294 static void setup_vm_with_server(IVState *s, int nvectors, bool msi)
296 char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
297 "-device ivshmem%s,chardev=chr0,vectors=%d",
299 msi ? "-doorbell" : ",size=1M,msi=off",
302 setup_vm_cmd(s, cmd, msi);
307 static void test_ivshmem_server(bool msi)
309 IVState state1, state2, *s1, *s2;
311 IvshmemServer server;
314 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
316 ret = ivshmem_server_init(&server, tmpserver, tmpshm, true,
317 TMPSHMSIZE, nvectors,
319 g_assert_cmpint(ret, ==, 0);
321 ret = ivshmem_server_start(&server);
322 g_assert_cmpint(ret, ==, 0);
324 thread.server = &server;
325 ret = pipe(thread.pipe);
326 g_assert_cmpint(ret, ==, 0);
327 thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
328 g_assert(thread.thread != NULL);
330 setup_vm_with_server(&state1, nvectors, msi);
332 setup_vm_with_server(&state2, nvectors, msi);
335 /* check got different VM ids */
336 vm1 = in_reg(s1, IVPOSITION);
337 vm2 = in_reg(s2, IVPOSITION);
338 g_assert_cmpint(vm1, >=, 0);
339 g_assert_cmpint(vm2, >=, 0);
340 g_assert_cmpint(vm1, !=, vm2);
342 /* check number of MSI-X vectors */
344 ret = qpci_msix_table_size(s1->dev);
345 g_assert_cmpuint(ret, ==, nvectors);
348 /* TODO test behavior before MSI-X is enabled */
350 /* ping vm2 -> vm1 on vector 0 */
352 ret = qpci_msix_pending(s1->dev, 0);
353 g_assert_cmpuint(ret, ==, 0);
355 g_assert_cmpuint(in_reg(s1, INTRSTATUS), ==, 0);
357 out_reg(s2, DOORBELL, vm1 << 16);
360 ret = msi ? qpci_msix_pending(s1->dev, 0) : in_reg(s1, INTRSTATUS);
361 } while (ret == 0 && g_get_monotonic_time() < end_time);
362 g_assert_cmpuint(ret, !=, 0);
364 /* ping vm1 -> vm2 on vector 1 */
366 ret = qpci_msix_pending(s2->dev, 1);
367 g_assert_cmpuint(ret, ==, 0);
369 g_assert_cmpuint(in_reg(s2, INTRSTATUS), ==, 0);
371 out_reg(s1, DOORBELL, vm2 << 16 | 1);
374 ret = msi ? qpci_msix_pending(s2->dev, 1) : in_reg(s2, INTRSTATUS);
375 } while (ret == 0 && g_get_monotonic_time() < end_time);
376 g_assert_cmpuint(ret, !=, 0);
381 if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
382 g_error("qemu_write_full: %s", g_strerror(errno));
385 g_thread_join(thread.thread);
387 ivshmem_server_close(&server);
388 close(thread.pipe[1]);
389 close(thread.pipe[0]);
392 static void test_ivshmem_server_msi(void)
394 test_ivshmem_server(true);
397 static void test_ivshmem_server_irq(void)
399 test_ivshmem_server(false);
402 #define PCI_SLOT_HP 0x06
404 static void test_ivshmem_hotplug(void)
406 const char *arch = qtest_get_arch();
410 qtest_qmp_device_add("ivshmem",
411 "iv1", "{'addr': %s, 'shm': %s, 'size': '1M'}",
412 stringify(PCI_SLOT_HP), tmpshm);
413 if (strcmp(arch, "ppc64") != 0) {
414 qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
420 static void test_ivshmem_memdev(void)
424 /* just for the sake of checking memory-backend property */
425 setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
426 " -device ivshmem-plain,memdev=mb1", false);
431 static void cleanup(void)
434 munmap(tmpshmem, TMPSHMSIZE);
456 static void abrt_handler(void *data)
461 static gchar *mktempshm(int size, int *fd)
466 name = g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
467 *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
468 S_IRWXU|S_IRWXG|S_IRWXO);
470 g_assert(ftruncate(*fd, size) == 0);
476 if (errno != EEXIST) {
483 int main(int argc, char **argv)
486 const char *arch = qtest_get_arch();
487 gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
489 g_test_init(&argc, &argv, NULL);
491 qtest_add_abrt_handler(abrt_handler, NULL);
493 tmpshm = mktempshm(TMPSHMSIZE, &fd);
497 tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
498 g_assert(tmpshmem != MAP_FAILED);
500 if (mkdtemp(dir) == NULL) {
501 g_error("mkdtemp: %s", g_strerror(errno));
504 tmpserver = g_strconcat(tmpdir, "/server", NULL);
506 qtest_add_func("/ivshmem/single", test_ivshmem_single);
507 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
508 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
510 qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
511 if (strcmp(arch, "ppc64") != 0) {
512 qtest_add_func("/ivshmem/server-msi", test_ivshmem_server_msi);
513 qtest_add_func("/ivshmem/server-irq", test_ivshmem_server_irq);