tests: Use qpci_mem{read,write} in ivshmem-test
[qemu/rayw.git] / tests / ivshmem-test.c
blobfbd8258c7b64b596038619546077e5a3f5cf4e39
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 "qemu/osdep.h"
12 #include <glib/gstdio.h>
13 #include "contrib/ivshmem-server/ivshmem-server.h"
14 #include "libqos/pci-pc.h"
15 #include "libqtest.h"
16 #include "qemu-common.h"
18 #define TMPSHMSIZE (1 << 20)
19 static char *tmpshm;
20 static void *tmpshmem;
21 static char *tmpdir;
22 static char *tmpserver;
24 static void save_fn(QPCIDevice *dev, int devfn, void *data)
26 QPCIDevice **pdev = (QPCIDevice **) data;
28 *pdev = dev;
31 static QPCIDevice *get_device(QPCIBus *pcibus)
33 QPCIDevice *dev;
35 dev = NULL;
36 qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
37 g_assert(dev != NULL);
39 return dev;
42 typedef struct _IVState {
43 QTestState *qtest;
44 void *reg_base, *mem_base;
45 QPCIBus *pcibus;
46 QPCIDevice *dev;
47 } IVState;
49 enum Reg {
50 INTRMASK = 0,
51 INTRSTATUS = 4,
52 IVPOSITION = 8,
53 DOORBELL = 12,
56 static const char* reg2str(enum Reg reg) {
57 switch (reg) {
58 case INTRMASK:
59 return "IntrMask";
60 case INTRSTATUS:
61 return "IntrStatus";
62 case IVPOSITION:
63 return "IVPosition";
64 case DOORBELL:
65 return "DoorBell";
66 default:
67 return NULL;
71 static inline unsigned in_reg(IVState *s, enum Reg reg)
73 const char *name = reg2str(reg);
74 QTestState *qtest = global_qtest;
75 unsigned res;
77 global_qtest = s->qtest;
78 res = qpci_io_readl(s->dev, s->reg_base + reg);
79 g_test_message("*%s -> %x\n", name, res);
80 global_qtest = qtest;
82 return 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_base + reg, v);
93 global_qtest = qtest;
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_base + 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_base + off, buf, len);
112 global_qtest = qtest;
115 static void cleanup_vm(IVState *s)
117 g_free(s->dev);
118 qpci_free_pc(s->pcibus);
119 qtest_quit(s->qtest);
122 static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
124 uint64_t barsize;
126 s->qtest = qtest_start(cmd);
127 s->pcibus = qpci_init_pc(NULL);
128 s->dev = get_device(s->pcibus);
130 s->reg_base = qpci_iomap(s->dev, 0, &barsize);
131 g_assert_nonnull(s->reg_base);
132 g_assert_cmpuint(barsize, ==, 256);
134 if (msix) {
135 qpci_msix_enable(s->dev);
138 s->mem_base = qpci_iomap(s->dev, 2, &barsize);
139 g_assert_nonnull(s->mem_base);
140 g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
142 qpci_device_enable(s->dev);
145 static void setup_vm(IVState *s)
147 char *cmd = g_strdup_printf("-object memory-backend-file"
148 ",id=mb1,size=1M,share,mem-path=/dev/shm%s"
149 " -device ivshmem-plain,memdev=mb1", tmpshm);
151 setup_vm_cmd(s, cmd, false);
153 g_free(cmd);
156 static void test_ivshmem_single(void)
158 IVState state, *s;
159 uint32_t data[1024];
160 int i;
162 setup_vm(&state);
163 s = &state;
165 /* initial state of readable registers */
166 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0);
167 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
168 g_assert_cmpuint(in_reg(s, IVPOSITION), ==, 0);
170 /* trigger interrupt via registers */
171 out_reg(s, INTRMASK, 0xffffffff);
172 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
173 out_reg(s, INTRSTATUS, 1);
174 /* check interrupt status */
175 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
176 /* reading clears */
177 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
178 /* TODO intercept actual interrupt (needs qtest work) */
180 /* invalid register access */
181 out_reg(s, IVPOSITION, 1);
182 in_reg(s, DOORBELL);
184 /* ring the (non-functional) doorbell */
185 out_reg(s, DOORBELL, 8 << 16);
187 /* write shared memory */
188 for (i = 0; i < G_N_ELEMENTS(data); i++) {
189 data[i] = i;
191 write_mem(s, 0, data, sizeof(data));
193 /* verify write */
194 for (i = 0; i < G_N_ELEMENTS(data); i++) {
195 g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
198 /* read it back and verify read */
199 memset(data, 0, sizeof(data));
200 read_mem(s, 0, data, sizeof(data));
201 for (i = 0; i < G_N_ELEMENTS(data); i++) {
202 g_assert_cmpuint(data[i], ==, i);
205 cleanup_vm(s);
208 static void test_ivshmem_pair(void)
210 IVState state1, state2, *s1, *s2;
211 char *data;
212 int i;
214 setup_vm(&state1);
215 s1 = &state1;
216 setup_vm(&state2);
217 s2 = &state2;
219 data = g_malloc0(TMPSHMSIZE);
221 /* host write, guest 1 & 2 read */
222 memset(tmpshmem, 0x42, TMPSHMSIZE);
223 read_mem(s1, 0, data, TMPSHMSIZE);
224 for (i = 0; i < TMPSHMSIZE; i++) {
225 g_assert_cmpuint(data[i], ==, 0x42);
227 read_mem(s2, 0, data, TMPSHMSIZE);
228 for (i = 0; i < TMPSHMSIZE; i++) {
229 g_assert_cmpuint(data[i], ==, 0x42);
232 /* guest 1 write, guest 2 read */
233 memset(data, 0x43, TMPSHMSIZE);
234 write_mem(s1, 0, data, TMPSHMSIZE);
235 memset(data, 0, TMPSHMSIZE);
236 read_mem(s2, 0, data, TMPSHMSIZE);
237 for (i = 0; i < TMPSHMSIZE; i++) {
238 g_assert_cmpuint(data[i], ==, 0x43);
241 /* guest 2 write, guest 1 read */
242 memset(data, 0x44, TMPSHMSIZE);
243 write_mem(s2, 0, data, TMPSHMSIZE);
244 memset(data, 0, TMPSHMSIZE);
245 read_mem(s1, 0, data, TMPSHMSIZE);
246 for (i = 0; i < TMPSHMSIZE; i++) {
247 g_assert_cmpuint(data[i], ==, 0x44);
250 cleanup_vm(s1);
251 cleanup_vm(s2);
252 g_free(data);
255 typedef struct ServerThread {
256 GThread *thread;
257 IvshmemServer *server;
258 int pipe[2]; /* to handle quit */
259 } ServerThread;
261 static void *server_thread(void *data)
263 ServerThread *t = data;
264 IvshmemServer *server = t->server;
266 while (true) {
267 fd_set fds;
268 int maxfd, ret;
270 FD_ZERO(&fds);
271 FD_SET(t->pipe[0], &fds);
272 maxfd = t->pipe[0] + 1;
274 ivshmem_server_get_fds(server, &fds, &maxfd);
276 ret = select(maxfd, &fds, NULL, NULL, NULL);
278 if (ret < 0) {
279 if (errno == EINTR) {
280 continue;
283 g_critical("select error: %s\n", strerror(errno));
284 break;
286 if (ret == 0) {
287 continue;
290 if (FD_ISSET(t->pipe[0], &fds)) {
291 break;
294 if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
295 g_critical("ivshmem_server_handle_fds() failed\n");
296 break;
300 return NULL;
303 static void setup_vm_with_server(IVState *s, int nvectors, bool msi)
305 char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
306 "-device ivshmem%s,chardev=chr0,vectors=%d",
307 tmpserver,
308 msi ? "-doorbell" : ",size=1M,msi=off",
309 nvectors);
311 setup_vm_cmd(s, cmd, msi);
313 g_free(cmd);
316 static void test_ivshmem_server(bool msi)
318 IVState state1, state2, *s1, *s2;
319 ServerThread thread;
320 IvshmemServer server;
321 int ret, vm1, vm2;
322 int nvectors = 2;
323 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
325 ret = ivshmem_server_init(&server, tmpserver, tmpshm, true,
326 TMPSHMSIZE, nvectors,
327 g_test_verbose());
328 g_assert_cmpint(ret, ==, 0);
330 ret = ivshmem_server_start(&server);
331 g_assert_cmpint(ret, ==, 0);
333 thread.server = &server;
334 ret = pipe(thread.pipe);
335 g_assert_cmpint(ret, ==, 0);
336 thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
337 g_assert(thread.thread != NULL);
339 setup_vm_with_server(&state1, nvectors, msi);
340 s1 = &state1;
341 setup_vm_with_server(&state2, nvectors, msi);
342 s2 = &state2;
344 /* check got different VM ids */
345 vm1 = in_reg(s1, IVPOSITION);
346 vm2 = in_reg(s2, IVPOSITION);
347 g_assert_cmpint(vm1, >=, 0);
348 g_assert_cmpint(vm2, >=, 0);
349 g_assert_cmpint(vm1, !=, vm2);
351 /* check number of MSI-X vectors */
352 global_qtest = s1->qtest;
353 if (msi) {
354 ret = qpci_msix_table_size(s1->dev);
355 g_assert_cmpuint(ret, ==, nvectors);
358 /* TODO test behavior before MSI-X is enabled */
360 /* ping vm2 -> vm1 on vector 0 */
361 if (msi) {
362 ret = qpci_msix_pending(s1->dev, 0);
363 g_assert_cmpuint(ret, ==, 0);
364 } else {
365 g_assert_cmpuint(in_reg(s1, INTRSTATUS), ==, 0);
367 out_reg(s2, DOORBELL, vm1 << 16);
368 do {
369 g_usleep(10000);
370 ret = msi ? qpci_msix_pending(s1->dev, 0) : in_reg(s1, INTRSTATUS);
371 } while (ret == 0 && g_get_monotonic_time() < end_time);
372 g_assert_cmpuint(ret, !=, 0);
374 /* ping vm1 -> vm2 on vector 1 */
375 global_qtest = s2->qtest;
376 if (msi) {
377 ret = qpci_msix_pending(s2->dev, 1);
378 g_assert_cmpuint(ret, ==, 0);
379 } else {
380 g_assert_cmpuint(in_reg(s2, INTRSTATUS), ==, 0);
382 out_reg(s1, DOORBELL, vm2 << 16 | 1);
383 do {
384 g_usleep(10000);
385 ret = msi ? qpci_msix_pending(s2->dev, 1) : in_reg(s2, INTRSTATUS);
386 } while (ret == 0 && g_get_monotonic_time() < end_time);
387 g_assert_cmpuint(ret, !=, 0);
389 cleanup_vm(s2);
390 cleanup_vm(s1);
392 if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
393 g_error("qemu_write_full: %s", g_strerror(errno));
396 g_thread_join(thread.thread);
398 ivshmem_server_close(&server);
399 close(thread.pipe[1]);
400 close(thread.pipe[0]);
403 static void test_ivshmem_server_msi(void)
405 test_ivshmem_server(true);
408 static void test_ivshmem_server_irq(void)
410 test_ivshmem_server(false);
413 #define PCI_SLOT_HP 0x06
415 static void test_ivshmem_hotplug(void)
417 gchar *opts;
419 qtest_start("");
421 opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
423 qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
424 qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
426 qtest_end();
427 g_free(opts);
430 static void test_ivshmem_memdev(void)
432 IVState state;
434 /* just for the sake of checking memory-backend property */
435 setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
436 " -device ivshmem-plain,memdev=mb1", false);
438 cleanup_vm(&state);
441 static void cleanup(void)
443 if (tmpshmem) {
444 munmap(tmpshmem, TMPSHMSIZE);
445 tmpshmem = NULL;
448 if (tmpshm) {
449 shm_unlink(tmpshm);
450 g_free(tmpshm);
451 tmpshm = NULL;
454 if (tmpserver) {
455 g_unlink(tmpserver);
456 g_free(tmpserver);
457 tmpserver = NULL;
460 if (tmpdir) {
461 g_rmdir(tmpdir);
462 tmpdir = NULL;
466 static void abrt_handler(void *data)
468 cleanup();
471 static gchar *mktempshm(int size, int *fd)
473 while (true) {
474 gchar *name;
476 name = g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
477 *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
478 S_IRWXU|S_IRWXG|S_IRWXO);
479 if (*fd > 0) {
480 g_assert(ftruncate(*fd, size) == 0);
481 return name;
484 g_free(name);
486 if (errno != EEXIST) {
487 perror("shm_open");
488 return NULL;
493 int main(int argc, char **argv)
495 int ret, fd;
496 gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
498 #if !GLIB_CHECK_VERSION(2, 31, 0)
499 if (!g_thread_supported()) {
500 g_thread_init(NULL);
502 #endif
504 g_test_init(&argc, &argv, NULL);
506 qtest_add_abrt_handler(abrt_handler, NULL);
507 /* shm */
508 tmpshm = mktempshm(TMPSHMSIZE, &fd);
509 if (!tmpshm) {
510 return 0;
512 tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
513 g_assert(tmpshmem != MAP_FAILED);
514 /* server */
515 if (mkdtemp(dir) == NULL) {
516 g_error("mkdtemp: %s", g_strerror(errno));
518 tmpdir = dir;
519 tmpserver = g_strconcat(tmpdir, "/server", NULL);
521 qtest_add_func("/ivshmem/single", test_ivshmem_single);
522 qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
523 qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
524 if (g_test_slow()) {
525 qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
526 qtest_add_func("/ivshmem/server-msi", test_ivshmem_server_msi);
527 qtest_add_func("/ivshmem/server-irq", test_ivshmem_server_irq);
530 ret = g_test_run();
532 cleanup();
534 return ret;