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/pci-pc.h"
16 #include "qemu-common.h"
18 #define TMPSHMSIZE (1 << 20)
20 static void *tmpshmem
;
22 static char *tmpserver
;
24 static void save_fn(QPCIDevice
*dev
, int devfn
, void *data
)
26 QPCIDevice
**pdev
= (QPCIDevice
**) data
;
31 static QPCIDevice
*get_device(QPCIBus
*pcibus
)
36 qpci_device_foreach(pcibus
, 0x1af4, 0x1110, save_fn
, &dev
);
37 g_assert(dev
!= NULL
);
42 typedef struct _IVState
{
44 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
);
74 QTestState
*qtest
= global_qtest
;
77 global_qtest
= s
->qtest
;
78 res
= qpci_io_readl(s
->dev
, s
->reg_bar
, reg
);
79 g_test_message("*%s -> %x\n", name
, res
);
85 static inline void out_reg(IVState
*s
, enum Reg reg
, unsigned v
)
87 const char *name
= reg2str(reg
);
88 QTestState
*qtest
= global_qtest
;
90 global_qtest
= s
->qtest
;
91 g_test_message("%x -> *%s\n", v
, name
);
92 qpci_io_writel(s
->dev
, s
->reg_bar
, reg
, v
);
96 static inline void read_mem(IVState
*s
, uint64_t off
, void *buf
, size_t len
)
98 QTestState
*qtest
= global_qtest
;
100 global_qtest
= s
->qtest
;
101 qpci_memread(s
->dev
, s
->mem_bar
, off
, buf
, len
);
102 global_qtest
= qtest
;
105 static inline void write_mem(IVState
*s
, uint64_t off
,
106 const void *buf
, size_t len
)
108 QTestState
*qtest
= global_qtest
;
110 global_qtest
= s
->qtest
;
111 qpci_memwrite(s
->dev
, s
->mem_bar
, off
, buf
, len
);
112 global_qtest
= qtest
;
115 static void cleanup_vm(IVState
*s
)
118 qpci_free_pc(s
->pcibus
);
119 qtest_quit(s
->qtest
);
122 static void setup_vm_cmd(IVState
*s
, const char *cmd
, bool msix
)
126 s
->qtest
= qtest_start(cmd
);
127 s
->pcibus
= qpci_init_pc(NULL
);
128 s
->dev
= get_device(s
->pcibus
);
130 s
->reg_bar
= qpci_iomap(s
->dev
, 0, &barsize
);
131 g_assert_cmpuint(barsize
, ==, 256);
134 qpci_msix_enable(s
->dev
);
137 s
->mem_bar
= qpci_iomap(s
->dev
, 2, &barsize
);
138 g_assert_cmpuint(barsize
, ==, TMPSHMSIZE
);
140 qpci_device_enable(s
->dev
);
143 static void setup_vm(IVState
*s
)
145 char *cmd
= g_strdup_printf("-object memory-backend-file"
146 ",id=mb1,size=1M,share,mem-path=/dev/shm%s"
147 " -device ivshmem-plain,memdev=mb1", tmpshm
);
149 setup_vm_cmd(s
, cmd
, false);
154 static void test_ivshmem_single(void)
163 /* initial state of readable registers */
164 g_assert_cmpuint(in_reg(s
, INTRMASK
), ==, 0);
165 g_assert_cmpuint(in_reg(s
, INTRSTATUS
), ==, 0);
166 g_assert_cmpuint(in_reg(s
, IVPOSITION
), ==, 0);
168 /* trigger interrupt via registers */
169 out_reg(s
, INTRMASK
, 0xffffffff);
170 g_assert_cmpuint(in_reg(s
, INTRMASK
), ==, 0xffffffff);
171 out_reg(s
, INTRSTATUS
, 1);
172 /* check interrupt status */
173 g_assert_cmpuint(in_reg(s
, INTRSTATUS
), ==, 1);
175 g_assert_cmpuint(in_reg(s
, INTRSTATUS
), ==, 0);
176 /* TODO intercept actual interrupt (needs qtest work) */
178 /* invalid register access */
179 out_reg(s
, IVPOSITION
, 1);
182 /* ring the (non-functional) doorbell */
183 out_reg(s
, DOORBELL
, 8 << 16);
185 /* write shared memory */
186 for (i
= 0; i
< G_N_ELEMENTS(data
); i
++) {
189 write_mem(s
, 0, data
, sizeof(data
));
192 for (i
= 0; i
< G_N_ELEMENTS(data
); i
++) {
193 g_assert_cmpuint(((uint32_t *)tmpshmem
)[i
], ==, i
);
196 /* read it back and verify read */
197 memset(data
, 0, sizeof(data
));
198 read_mem(s
, 0, data
, sizeof(data
));
199 for (i
= 0; i
< G_N_ELEMENTS(data
); i
++) {
200 g_assert_cmpuint(data
[i
], ==, i
);
206 static void test_ivshmem_pair(void)
208 IVState state1
, state2
, *s1
, *s2
;
217 data
= g_malloc0(TMPSHMSIZE
);
219 /* host write, guest 1 & 2 read */
220 memset(tmpshmem
, 0x42, TMPSHMSIZE
);
221 read_mem(s1
, 0, data
, TMPSHMSIZE
);
222 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
223 g_assert_cmpuint(data
[i
], ==, 0x42);
225 read_mem(s2
, 0, data
, TMPSHMSIZE
);
226 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
227 g_assert_cmpuint(data
[i
], ==, 0x42);
230 /* guest 1 write, guest 2 read */
231 memset(data
, 0x43, TMPSHMSIZE
);
232 write_mem(s1
, 0, data
, TMPSHMSIZE
);
233 memset(data
, 0, TMPSHMSIZE
);
234 read_mem(s2
, 0, data
, TMPSHMSIZE
);
235 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
236 g_assert_cmpuint(data
[i
], ==, 0x43);
239 /* guest 2 write, guest 1 read */
240 memset(data
, 0x44, TMPSHMSIZE
);
241 write_mem(s2
, 0, data
, TMPSHMSIZE
);
242 memset(data
, 0, TMPSHMSIZE
);
243 read_mem(s1
, 0, data
, TMPSHMSIZE
);
244 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
245 g_assert_cmpuint(data
[i
], ==, 0x44);
253 typedef struct ServerThread
{
255 IvshmemServer
*server
;
256 int pipe
[2]; /* to handle quit */
259 static void *server_thread(void *data
)
261 ServerThread
*t
= data
;
262 IvshmemServer
*server
= t
->server
;
269 FD_SET(t
->pipe
[0], &fds
);
270 maxfd
= t
->pipe
[0] + 1;
272 ivshmem_server_get_fds(server
, &fds
, &maxfd
);
274 ret
= select(maxfd
, &fds
, NULL
, NULL
, NULL
);
277 if (errno
== EINTR
) {
281 g_critical("select error: %s\n", strerror(errno
));
288 if (FD_ISSET(t
->pipe
[0], &fds
)) {
292 if (ivshmem_server_handle_fds(server
, &fds
, maxfd
) < 0) {
293 g_critical("ivshmem_server_handle_fds() failed\n");
301 static void setup_vm_with_server(IVState
*s
, int nvectors
, bool msi
)
303 char *cmd
= g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
304 "-device ivshmem%s,chardev=chr0,vectors=%d",
306 msi
? "-doorbell" : ",size=1M,msi=off",
309 setup_vm_cmd(s
, cmd
, msi
);
314 static void test_ivshmem_server(bool msi
)
316 IVState state1
, state2
, *s1
, *s2
;
318 IvshmemServer server
;
321 guint64 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
323 ret
= ivshmem_server_init(&server
, tmpserver
, tmpshm
, true,
324 TMPSHMSIZE
, nvectors
,
326 g_assert_cmpint(ret
, ==, 0);
328 ret
= ivshmem_server_start(&server
);
329 g_assert_cmpint(ret
, ==, 0);
331 thread
.server
= &server
;
332 ret
= pipe(thread
.pipe
);
333 g_assert_cmpint(ret
, ==, 0);
334 thread
.thread
= g_thread_new("ivshmem-server", server_thread
, &thread
);
335 g_assert(thread
.thread
!= NULL
);
337 setup_vm_with_server(&state1
, nvectors
, msi
);
339 setup_vm_with_server(&state2
, nvectors
, msi
);
342 /* check got different VM ids */
343 vm1
= in_reg(s1
, IVPOSITION
);
344 vm2
= in_reg(s2
, IVPOSITION
);
345 g_assert_cmpint(vm1
, >=, 0);
346 g_assert_cmpint(vm2
, >=, 0);
347 g_assert_cmpint(vm1
, !=, vm2
);
349 /* check number of MSI-X vectors */
350 global_qtest
= s1
->qtest
;
352 ret
= qpci_msix_table_size(s1
->dev
);
353 g_assert_cmpuint(ret
, ==, nvectors
);
356 /* TODO test behavior before MSI-X is enabled */
358 /* ping vm2 -> vm1 on vector 0 */
360 ret
= qpci_msix_pending(s1
->dev
, 0);
361 g_assert_cmpuint(ret
, ==, 0);
363 g_assert_cmpuint(in_reg(s1
, INTRSTATUS
), ==, 0);
365 out_reg(s2
, DOORBELL
, vm1
<< 16);
368 ret
= msi
? qpci_msix_pending(s1
->dev
, 0) : in_reg(s1
, INTRSTATUS
);
369 } while (ret
== 0 && g_get_monotonic_time() < end_time
);
370 g_assert_cmpuint(ret
, !=, 0);
372 /* ping vm1 -> vm2 on vector 1 */
373 global_qtest
= s2
->qtest
;
375 ret
= qpci_msix_pending(s2
->dev
, 1);
376 g_assert_cmpuint(ret
, ==, 0);
378 g_assert_cmpuint(in_reg(s2
, INTRSTATUS
), ==, 0);
380 out_reg(s1
, DOORBELL
, vm2
<< 16 | 1);
383 ret
= msi
? qpci_msix_pending(s2
->dev
, 1) : in_reg(s2
, INTRSTATUS
);
384 } while (ret
== 0 && g_get_monotonic_time() < end_time
);
385 g_assert_cmpuint(ret
, !=, 0);
390 if (qemu_write_full(thread
.pipe
[1], "q", 1) != 1) {
391 g_error("qemu_write_full: %s", g_strerror(errno
));
394 g_thread_join(thread
.thread
);
396 ivshmem_server_close(&server
);
397 close(thread
.pipe
[1]);
398 close(thread
.pipe
[0]);
401 static void test_ivshmem_server_msi(void)
403 test_ivshmem_server(true);
406 static void test_ivshmem_server_irq(void)
408 test_ivshmem_server(false);
411 #define PCI_SLOT_HP 0x06
413 static void test_ivshmem_hotplug(void)
419 opts
= g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm
);
421 qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP
, opts
);
422 qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP
);
428 static void test_ivshmem_memdev(void)
432 /* just for the sake of checking memory-backend property */
433 setup_vm_cmd(&state
, "-object memory-backend-ram,size=1M,id=mb1"
434 " -device ivshmem-plain,memdev=mb1", false);
439 static void cleanup(void)
442 munmap(tmpshmem
, TMPSHMSIZE
);
464 static void abrt_handler(void *data
)
469 static gchar
*mktempshm(int size
, int *fd
)
474 name
= g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
475 *fd
= shm_open(name
, O_CREAT
|O_RDWR
|O_EXCL
,
476 S_IRWXU
|S_IRWXG
|S_IRWXO
);
478 g_assert(ftruncate(*fd
, size
) == 0);
484 if (errno
!= EEXIST
) {
491 int main(int argc
, char **argv
)
494 gchar dir
[] = "/tmp/ivshmem-test.XXXXXX";
496 #if !GLIB_CHECK_VERSION(2, 31, 0)
497 if (!g_thread_supported()) {
502 g_test_init(&argc
, &argv
, NULL
);
504 qtest_add_abrt_handler(abrt_handler
, NULL
);
506 tmpshm
= mktempshm(TMPSHMSIZE
, &fd
);
510 tmpshmem
= mmap(0, TMPSHMSIZE
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
511 g_assert(tmpshmem
!= MAP_FAILED
);
513 if (mkdtemp(dir
) == NULL
) {
514 g_error("mkdtemp: %s", g_strerror(errno
));
517 tmpserver
= g_strconcat(tmpdir
, "/server", NULL
);
519 qtest_add_func("/ivshmem/single", test_ivshmem_single
);
520 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug
);
521 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev
);
523 qtest_add_func("/ivshmem/pair", test_ivshmem_pair
);
524 qtest_add_func("/ivshmem/server-msi", test_ivshmem_server_msi
);
525 qtest_add_func("/ivshmem/server-irq", test_ivshmem_server_irq
);