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"
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
;
55 static const char* reg2str(enum Reg reg
) {
70 static inline unsigned in_reg(IVState
*s
, enum Reg reg
)
72 const char *name
= reg2str(reg
);
75 res
= qpci_io_readl(s
->dev
, s
->reg_bar
, reg
);
76 g_test_message("*%s -> %x", name
, res
);
81 static inline void out_reg(IVState
*s
, enum Reg reg
, unsigned v
)
83 const char *name
= reg2str(reg
);
85 g_test_message("%x -> *%s", v
, name
);
86 qpci_io_writel(s
->dev
, s
->reg_bar
, reg
, v
);
89 static inline void read_mem(IVState
*s
, uint64_t off
, void *buf
, size_t len
)
91 qpci_memread(s
->dev
, s
->mem_bar
, off
, buf
, len
);
94 static inline void write_mem(IVState
*s
, uint64_t off
,
95 const void *buf
, size_t len
)
97 qpci_memwrite(s
->dev
, s
->mem_bar
, off
, buf
, len
);
100 static void cleanup_vm(IVState
*s
)
103 qtest_shutdown(s
->qs
);
106 static void setup_vm_cmd(IVState
*s
, const char *cmd
, bool msix
)
109 const char *arch
= qtest_get_arch();
111 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
112 s
->qs
= qtest_pc_boot("%s", cmd
);
113 } else if (strcmp(arch
, "ppc64") == 0) {
114 s
->qs
= qtest_spapr_boot("%s", cmd
);
116 g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
119 s
->dev
= get_device(s
->qs
->pcibus
);
121 s
->reg_bar
= qpci_iomap(s
->dev
, 0, &barsize
);
122 g_assert_cmpuint(barsize
, ==, 256);
125 qpci_msix_enable(s
->dev
);
128 s
->mem_bar
= qpci_iomap(s
->dev
, 2, &barsize
);
129 g_assert_cmpuint(barsize
, ==, TMPSHMSIZE
);
131 qpci_device_enable(s
->dev
);
134 static void setup_vm(IVState
*s
)
136 char *cmd
= g_strdup_printf("-object memory-backend-file"
137 ",id=mb1,size=1M,share=on,mem-path=/dev/shm%s"
138 " -device ivshmem-plain,memdev=mb1", tmpshm
);
140 setup_vm_cmd(s
, cmd
, false);
145 static void test_ivshmem_single(void)
154 /* initial state of readable registers */
155 g_assert_cmpuint(in_reg(s
, INTRMASK
), ==, 0);
156 g_assert_cmpuint(in_reg(s
, INTRSTATUS
), ==, 0);
157 g_assert_cmpuint(in_reg(s
, IVPOSITION
), ==, 0);
159 /* trigger interrupt via registers */
160 out_reg(s
, INTRMASK
, 0xffffffff);
161 g_assert_cmpuint(in_reg(s
, INTRMASK
), ==, 0xffffffff);
162 out_reg(s
, INTRSTATUS
, 1);
163 /* check interrupt status */
164 g_assert_cmpuint(in_reg(s
, INTRSTATUS
), ==, 1);
166 g_assert_cmpuint(in_reg(s
, INTRSTATUS
), ==, 0);
167 /* TODO intercept actual interrupt (needs qtest work) */
169 /* invalid register access */
170 out_reg(s
, IVPOSITION
, 1);
173 /* ring the (non-functional) doorbell */
174 out_reg(s
, DOORBELL
, 8 << 16);
176 /* write shared memory */
177 for (i
= 0; i
< G_N_ELEMENTS(data
); i
++) {
180 write_mem(s
, 0, data
, sizeof(data
));
183 for (i
= 0; i
< G_N_ELEMENTS(data
); i
++) {
184 g_assert_cmpuint(((uint32_t *)tmpshmem
)[i
], ==, i
);
187 /* read it back and verify read */
188 memset(data
, 0, sizeof(data
));
189 read_mem(s
, 0, data
, sizeof(data
));
190 for (i
= 0; i
< G_N_ELEMENTS(data
); i
++) {
191 g_assert_cmpuint(data
[i
], ==, i
);
197 static void test_ivshmem_pair(void)
199 IVState state1
, state2
, *s1
, *s2
;
208 data
= g_malloc0(TMPSHMSIZE
);
210 /* host write, guest 1 & 2 read */
211 memset(tmpshmem
, 0x42, TMPSHMSIZE
);
212 read_mem(s1
, 0, data
, TMPSHMSIZE
);
213 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
214 g_assert_cmpuint(data
[i
], ==, 0x42);
216 read_mem(s2
, 0, data
, TMPSHMSIZE
);
217 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
218 g_assert_cmpuint(data
[i
], ==, 0x42);
221 /* guest 1 write, guest 2 read */
222 memset(data
, 0x43, TMPSHMSIZE
);
223 write_mem(s1
, 0, data
, TMPSHMSIZE
);
224 memset(data
, 0, TMPSHMSIZE
);
225 read_mem(s2
, 0, data
, TMPSHMSIZE
);
226 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
227 g_assert_cmpuint(data
[i
], ==, 0x43);
230 /* guest 2 write, guest 1 read */
231 memset(data
, 0x44, TMPSHMSIZE
);
232 write_mem(s2
, 0, data
, TMPSHMSIZE
);
233 memset(data
, 0, TMPSHMSIZE
);
234 read_mem(s1
, 0, data
, TMPSHMSIZE
);
235 for (i
= 0; i
< TMPSHMSIZE
; i
++) {
236 g_assert_cmpuint(data
[i
], ==, 0x44);
244 typedef struct ServerThread
{
246 IvshmemServer
*server
;
247 int pipe
[2]; /* to handle quit */
250 static void *server_thread(void *data
)
252 ServerThread
*t
= data
;
253 IvshmemServer
*server
= t
->server
;
260 FD_SET(t
->pipe
[0], &fds
);
261 maxfd
= t
->pipe
[0] + 1;
263 ivshmem_server_get_fds(server
, &fds
, &maxfd
);
265 ret
= select(maxfd
, &fds
, NULL
, NULL
, NULL
);
268 if (errno
== EINTR
) {
272 g_critical("select error: %s\n", strerror(errno
));
279 if (FD_ISSET(t
->pipe
[0], &fds
)) {
283 if (ivshmem_server_handle_fds(server
, &fds
, maxfd
) < 0) {
284 g_critical("ivshmem_server_handle_fds() failed\n");
292 static void setup_vm_with_server(IVState
*s
, int nvectors
)
296 cmd
= g_strdup_printf("-chardev socket,id=chr0,path=%s "
297 "-device ivshmem-doorbell,chardev=chr0,vectors=%d",
298 tmpserver
, nvectors
);
300 setup_vm_cmd(s
, cmd
, true);
305 static void test_ivshmem_server(void)
307 g_autoptr(GError
) err
= NULL
;
308 IVState state1
, state2
, *s1
, *s2
;
310 IvshmemServer server
;
313 guint64 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
315 ret
= ivshmem_server_init(&server
, tmpserver
, tmpshm
, true,
316 TMPSHMSIZE
, nvectors
,
318 g_assert_cmpint(ret
, ==, 0);
320 ret
= ivshmem_server_start(&server
);
321 g_assert_cmpint(ret
, ==, 0);
323 thread
.server
= &server
;
324 g_unix_open_pipe(thread
.pipe
, FD_CLOEXEC
, &err
);
325 g_assert_no_error(err
);
326 thread
.thread
= g_thread_new("ivshmem-server", server_thread
, &thread
);
327 g_assert(thread
.thread
!= NULL
);
329 setup_vm_with_server(&state1
, nvectors
);
331 setup_vm_with_server(&state2
, nvectors
);
334 /* check got different VM ids */
335 vm1
= in_reg(s1
, IVPOSITION
);
336 vm2
= in_reg(s2
, IVPOSITION
);
337 g_assert_cmpint(vm1
, >=, 0);
338 g_assert_cmpint(vm2
, >=, 0);
339 g_assert_cmpint(vm1
, !=, vm2
);
341 /* check number of MSI-X vectors */
342 ret
= qpci_msix_table_size(s1
->dev
);
343 g_assert_cmpuint(ret
, ==, nvectors
);
345 /* TODO test behavior before MSI-X is enabled */
347 /* ping vm2 -> vm1 on vector 0 */
348 ret
= qpci_msix_pending(s1
->dev
, 0);
349 g_assert_cmpuint(ret
, ==, 0);
350 out_reg(s2
, DOORBELL
, vm1
<< 16);
353 ret
= qpci_msix_pending(s1
->dev
, 0);
354 } while (ret
== 0 && g_get_monotonic_time() < end_time
);
355 g_assert_cmpuint(ret
, !=, 0);
357 /* ping vm1 -> vm2 on vector 1 */
358 ret
= qpci_msix_pending(s2
->dev
, 1);
359 g_assert_cmpuint(ret
, ==, 0);
360 out_reg(s1
, DOORBELL
, vm2
<< 16 | 1);
363 ret
= qpci_msix_pending(s2
->dev
, 1);
364 } while (ret
== 0 && g_get_monotonic_time() < end_time
);
365 g_assert_cmpuint(ret
, !=, 0);
370 if (qemu_write_full(thread
.pipe
[1], "q", 1) != 1) {
371 g_error("qemu_write_full: %s", g_strerror(errno
));
374 g_thread_join(thread
.thread
);
376 ivshmem_server_close(&server
);
377 close(thread
.pipe
[1]);
378 close(thread
.pipe
[0]);
381 static void test_ivshmem_hotplug_q35(void)
383 QTestState
*qts
= qtest_init("-object memory-backend-ram,size=1M,id=mb1 "
384 "-device pcie-root-port,id=p1 "
385 "-device pcie-pci-bridge,bus=p1,id=b1 "
388 qtest_qmp_device_add(qts
, "ivshmem-plain", "iv1",
389 "{'memdev': 'mb1', 'bus': 'b1'}");
390 qtest_qmp_device_del_send(qts
, "iv1");
395 #define PCI_SLOT_HP 0x06
397 static void test_ivshmem_hotplug(void)
400 const char *arch
= qtest_get_arch();
402 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
403 qts
= qtest_init("-object memory-backend-ram,size=1M,id=mb1"
406 qts
= qtest_init("-object memory-backend-ram,size=1M,id=mb1");
409 qtest_qmp_device_add(qts
, "ivshmem-plain", "iv1",
410 "{'addr': %s, 'memdev': 'mb1'}",
411 stringify(PCI_SLOT_HP
));
412 if (strcmp(arch
, "ppc64") != 0) {
413 qpci_unplug_acpi_device_test(qts
, "iv1", PCI_SLOT_HP
);
419 static void test_ivshmem_memdev(void)
423 /* just for the sake of checking memory-backend property */
424 setup_vm_cmd(&state
, "-object memory-backend-ram,size=1M,id=mb1"
425 " -device ivshmem-plain,memdev=mb1", false);
430 static void cleanup(void)
433 munmap(tmpshmem
, TMPSHMSIZE
);
455 static void abrt_handler(void *data
)
460 static gchar
*mktempshm(int size
, int *fd
)
465 name
= g_strdup_printf("/qtest-%u-%u", getpid(), g_test_rand_int());
466 *fd
= shm_open(name
, O_CREAT
|O_RDWR
|O_EXCL
,
467 S_IRWXU
|S_IRWXG
|S_IRWXO
);
469 g_assert(ftruncate(*fd
, size
) == 0);
475 if (errno
!= EEXIST
) {
482 int main(int argc
, char **argv
)
485 gchar dir
[] = "/tmp/ivshmem-test.XXXXXX";
486 const char *arch
= qtest_get_arch();
488 g_test_init(&argc
, &argv
, NULL
);
490 qtest_add_abrt_handler(abrt_handler
, NULL
);
492 tmpshm
= mktempshm(TMPSHMSIZE
, &fd
);
496 tmpshmem
= mmap(0, TMPSHMSIZE
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
497 g_assert(tmpshmem
!= MAP_FAILED
);
499 if (g_mkdtemp(dir
) == NULL
) {
500 g_error("g_mkdtemp: %s", g_strerror(errno
));
503 tmpserver
= g_strconcat(tmpdir
, "/server", NULL
);
505 qtest_add_func("/ivshmem/single", test_ivshmem_single
);
506 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug
);
507 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev
);
509 qtest_add_func("/ivshmem/pair", test_ivshmem_pair
);
510 qtest_add_func("/ivshmem/server", test_ivshmem_server
);
512 if (!strcmp(arch
, "x86_64") && qtest_has_machine("q35")) {
513 qtest_add_func("/ivshmem/hotplug-q35", test_ivshmem_hotplug_q35
);