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"
13 #include <glib/gstdio.h>
15 #include "contrib/ivshmem-server/ivshmem-server.h"
16 #include "libqos/pci-pc.h"
18 #include "qemu-common.h"
20 #define TMPSHMSIZE (1 << 20)
22 static void *tmpshmem
;
24 static char *tmpserver
;
26 static void save_fn(QPCIDevice
*dev
, int devfn
, void *data
)
28 QPCIDevice
**pdev
= (QPCIDevice
**) data
;
33 static QPCIDevice
*get_device(QPCIBus
*pcibus
)
38 qpci_device_foreach(pcibus
, 0x1af4, 0x1110, save_fn
, &dev
);
39 g_assert(dev
!= NULL
);
44 typedef struct _IVState
{
46 void *reg_base
, *mem_base
;
58 static const char* reg2str(enum Reg reg
) {
73 static inline unsigned in_reg(IVState
*s
, enum Reg reg
)
75 const char *name
= reg2str(reg
);
76 QTestState
*qtest
= global_qtest
;
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
);
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
);
98 static void cleanup_vm(IVState
*s
)
101 qpci_free_pc(s
->pcibus
);
102 qtest_quit(s
->qtest
);
105 static void setup_vm_cmd(IVState
*s
, const char *cmd
, bool msix
)
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
);
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);
138 static void test_ivshmem_single(void)
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);
159 out_reg(s
, IVPOSITION
, 1);
160 out_reg(s
, DOORBELL
, 8 << 16);
162 for (i
= 0; i
< G_N_ELEMENTS(data
); 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
);
181 static void test_ivshmem_pair(void)
183 IVState state1
, state2
, *s1
, *s2
;
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);
228 typedef struct ServerThread
{
230 IvshmemServer
*server
;
231 int pipe
[2]; /* to handle quit */
234 static void *server_thread(void *data
)
236 ServerThread
*t
= data
;
237 IvshmemServer
*server
= t
->server
;
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
);
252 if (errno
== EINTR
) {
256 g_critical("select error: %s\n", strerror(errno
));
263 if (FD_ISSET(t
->pipe
[0], &fds
)) {
267 if (ivshmem_server_handle_fds(server
, &fds
, maxfd
) < 0) {
268 g_critical("ivshmem_server_handle_fds() failed\n");
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
);
287 static void test_ivshmem_server(bool msi
)
289 IVState state1
, state2
, *s1
, *s2
;
291 IvshmemServer server
;
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
,
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
);
307 setup_vm_with_server(&state2
, nvectors
, msi
);
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
) {
325 if (qtest_readb(s1
->qtest
, (uintptr_t)s1
->mem_base
) == 0x42 &&
326 qtest_readb(s2
->qtest
, (uintptr_t)s2
->mem_base
) == 0x42) {
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
;
338 ret
= qpci_msix_table_size(s1
->dev
);
339 g_assert_cmpuint(ret
, ==, nvectors
);
342 /* ping vm2 -> vm1 */
344 ret
= qpci_msix_pending(s1
->dev
, 0);
345 g_assert_cmpuint(ret
, ==, 0);
347 out_reg(s1
, INTRSTATUS
, 0);
349 out_reg(s2
, DOORBELL
, vm1
<< 16);
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
;
359 ret
= qpci_msix_pending(s2
->dev
, 0);
360 g_assert_cmpuint(ret
, ==, 0);
362 out_reg(s2
, INTRSTATUS
, 0);
364 out_reg(s1
, DOORBELL
, vm2
<< 16);
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);
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)
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
);
412 static void test_ivshmem_memdev(void)
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);
423 static void cleanup(void)
426 munmap(tmpshmem
, TMPSHMSIZE
);
448 static void abrt_handler(void *data
)
453 static gchar
*mktempshm(int size
, int *fd
)
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
);
462 g_assert(ftruncate(*fd
, size
) == 0);
468 if (errno
!= EEXIST
) {
475 int main(int argc
, char **argv
)
478 gchar dir
[] = "/tmp/ivshmem-test.XXXXXX";
480 #if !GLIB_CHECK_VERSION(2, 31, 0)
481 if (!g_thread_supported()) {
486 g_test_init(&argc
, &argv
, NULL
);
488 qtest_add_abrt_handler(abrt_handler
, NULL
);
490 tmpshm
= mktempshm(TMPSHMSIZE
, &fd
);
494 tmpshmem
= mmap(0, TMPSHMSIZE
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
495 g_assert(tmpshmem
!= MAP_FAILED
);
497 if (mkdtemp(dir
) == NULL
) {
498 g_error("mkdtemp: %s", g_strerror(errno
));
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
);
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
);