pci core: assert ENOSPC when add capability
[qemu/ar7.git] / tests / ivshmem-test.c
blob010860a5b728858acef2f9a44c4aa621fe926704
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 <sys/mman.h>
14 #include "contrib/ivshmem-server/ivshmem-server.h"
15 #include "libqos/pci-pc.h"
16 #include "libqtest.h"
17 #include "qemu-common.h"
19 #define TMPSHMSIZE (1 << 20)
20 static char *tmpshm;
21 static void *tmpshmem;
22 static char *tmpdir;
23 static char *tmpserver;
25 static void save_fn(QPCIDevice *dev, int devfn, void *data)
27 QPCIDevice **pdev = (QPCIDevice **) data;
29 *pdev = dev;
32 static QPCIDevice *get_device(QPCIBus *pcibus)
34 QPCIDevice *dev;
36 dev = NULL;
37 qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
38 g_assert(dev != NULL);
40 return dev;
43 typedef struct _IVState {
44 QTestState *qtest;
45 void *reg_base, *mem_base;
46 QPCIBus *pcibus;
47 QPCIDevice *dev;
48 } IVState;
50 enum Reg {
51 INTRMASK = 0,
52 INTRSTATUS = 4,
53 IVPOSITION = 8,
54 DOORBELL = 12,
57 static const char* reg2str(enum Reg reg) {
58 switch (reg) {
59 case INTRMASK:
60 return "IntrMask";
61 case INTRSTATUS:
62 return "IntrStatus";
63 case IVPOSITION:
64 return "IVPosition";
65 case DOORBELL:
66 return "DoorBell";
67 default:
68 return NULL;
72 static inline unsigned in_reg(IVState *s, enum Reg reg)
74 const char *name = reg2str(reg);
75 QTestState *qtest = global_qtest;
76 unsigned res;
78 global_qtest = s->qtest;
79 res = qpci_io_readl(s->dev, s->reg_base + reg);
80 g_test_message("*%s -> %x\n", name, res);
81 global_qtest = qtest;
83 return res;
86 static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
88 const char *name = reg2str(reg);
89 QTestState *qtest = global_qtest;
91 global_qtest = s->qtest;
92 g_test_message("%x -> *%s\n", v, name);
93 qpci_io_writel(s->dev, s->reg_base + reg, v);
94 global_qtest = qtest;
97 static void cleanup_vm(IVState *s)
99 g_free(s->dev);
100 qpci_free_pc(s->pcibus);
101 qtest_quit(s->qtest);
104 static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
106 uint64_t barsize;
108 s->qtest = qtest_start(cmd);
109 s->pcibus = qpci_init_pc();
110 s->dev = get_device(s->pcibus);
112 s->reg_base = qpci_iomap(s->dev, 0, &barsize);
113 g_assert_nonnull(s->reg_base);
114 g_assert_cmpuint(barsize, ==, 256);
116 if (msix) {
117 qpci_msix_enable(s->dev);
120 s->mem_base = qpci_iomap(s->dev, 2, &barsize);
121 g_assert_nonnull(s->mem_base);
122 g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
124 qpci_device_enable(s->dev);
127 static void setup_vm(IVState *s)
129 char *cmd = g_strdup_printf("-object memory-backend-file"
130 ",id=mb1,size=1M,share,mem-path=/dev/shm%s"
131 " -device ivshmem-plain,memdev=mb1", tmpshm);
133 setup_vm_cmd(s, cmd, false);
135 g_free(cmd);
138 static void test_ivshmem_single(void)
140 IVState state, *s;
141 uint32_t data[1024];
142 int i;
144 setup_vm(&state);
145 s = &state;
147 /* initial state of readable registers */
148 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0);
149 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
150 g_assert_cmpuint(in_reg(s, IVPOSITION), ==, 0);
152 /* trigger interrupt via registers */
153 out_reg(s, INTRMASK, 0xffffffff);
154 g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
155 out_reg(s, INTRSTATUS, 1);
156 /* check interrupt status */
157 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
158 /* reading clears */
159 g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
160 /* TODO intercept actual interrupt (needs qtest work) */
162 /* invalid register access */
163 out_reg(s, IVPOSITION, 1);
164 in_reg(s, DOORBELL);
166 /* ring the (non-functional) doorbell */
167 out_reg(s, DOORBELL, 8 << 16);
169 /* write shared memory */
170 for (i = 0; i < G_N_ELEMENTS(data); i++) {
171 data[i] = i;
173 qtest_memwrite(s->qtest, (uintptr_t)s->mem_base, data, sizeof(data));
175 /* verify write */
176 for (i = 0; i < G_N_ELEMENTS(data); i++) {
177 g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
180 /* read it back and verify read */
181 memset(data, 0, sizeof(data));
182 qtest_memread(s->qtest, (uintptr_t)s->mem_base, data, sizeof(data));
183 for (i = 0; i < G_N_ELEMENTS(data); i++) {
184 g_assert_cmpuint(data[i], ==, i);
187 cleanup_vm(s);
190 static void test_ivshmem_pair(void)
192 IVState state1, state2, *s1, *s2;
193 char *data;
194 int i;
196 setup_vm(&state1);
197 s1 = &state1;
198 setup_vm(&state2);
199 s2 = &state2;
201 data = g_malloc0(TMPSHMSIZE);
203 /* host write, guest 1 & 2 read */
204 memset(tmpshmem, 0x42, TMPSHMSIZE);
205 qtest_memread(s1->qtest, (uintptr_t)s1->mem_base, data, TMPSHMSIZE);
206 for (i = 0; i < TMPSHMSIZE; i++) {
207 g_assert_cmpuint(data[i], ==, 0x42);
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], ==, 0x42);
214 /* guest 1 write, guest 2 read */
215 memset(data, 0x43, TMPSHMSIZE);
216 qtest_memwrite(s1->qtest, (uintptr_t)s1->mem_base, data, TMPSHMSIZE);
217 memset(data, 0, TMPSHMSIZE);
218 qtest_memread(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
219 for (i = 0; i < TMPSHMSIZE; i++) {
220 g_assert_cmpuint(data[i], ==, 0x43);
223 /* guest 2 write, guest 1 read */
224 memset(data, 0x44, TMPSHMSIZE);
225 qtest_memwrite(s2->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
226 memset(data, 0, TMPSHMSIZE);
227 qtest_memread(s1->qtest, (uintptr_t)s2->mem_base, data, TMPSHMSIZE);
228 for (i = 0; i < TMPSHMSIZE; i++) {
229 g_assert_cmpuint(data[i], ==, 0x44);
232 cleanup_vm(s1);
233 cleanup_vm(s2);
234 g_free(data);
237 typedef struct ServerThread {
238 GThread *thread;
239 IvshmemServer *server;
240 int pipe[2]; /* to handle quit */
241 } ServerThread;
243 static void *server_thread(void *data)
245 ServerThread *t = data;
246 IvshmemServer *server = t->server;
248 while (true) {
249 fd_set fds;
250 int maxfd, ret;
252 FD_ZERO(&fds);
253 FD_SET(t->pipe[0], &fds);
254 maxfd = t->pipe[0] + 1;
256 ivshmem_server_get_fds(server, &fds, &maxfd);
258 ret = select(maxfd, &fds, NULL, NULL, NULL);
260 if (ret < 0) {
261 if (errno == EINTR) {
262 continue;
265 g_critical("select error: %s\n", strerror(errno));
266 break;
268 if (ret == 0) {
269 continue;
272 if (FD_ISSET(t->pipe[0], &fds)) {
273 break;
276 if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
277 g_critical("ivshmem_server_handle_fds() failed\n");
278 break;
282 return NULL;
285 static void setup_vm_with_server(IVState *s, int nvectors, bool msi)
287 char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
288 "-device ivshmem%s,chardev=chr0,vectors=%d",
289 tmpserver,
290 msi ? "-doorbell" : ",size=1M,msi=off",
291 nvectors);
293 setup_vm_cmd(s, cmd, msi);
295 g_free(cmd);
298 static void test_ivshmem_server(bool msi)
300 IVState state1, state2, *s1, *s2;
301 ServerThread thread;
302 IvshmemServer server;
303 int ret, vm1, vm2;
304 int nvectors = 2;
305 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
307 ret = ivshmem_server_init(&server, tmpserver, tmpshm, true,
308 TMPSHMSIZE, nvectors,
309 g_test_verbose());
310 g_assert_cmpint(ret, ==, 0);
312 ret = ivshmem_server_start(&server);
313 g_assert_cmpint(ret, ==, 0);
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 setup_vm_with_server(&state1, nvectors, msi);
322 s1 = &state1;
323 setup_vm_with_server(&state2, nvectors, msi);
324 s2 = &state2;
326 /* check got different VM ids */
327 vm1 = in_reg(s1, IVPOSITION);
328 vm2 = in_reg(s2, IVPOSITION);
329 g_assert_cmpint(vm1, >=, 0);
330 g_assert_cmpint(vm2, >=, 0);
331 g_assert_cmpint(vm1, !=, vm2);
333 /* check number of MSI-X vectors */
334 global_qtest = s1->qtest;
335 if (msi) {
336 ret = qpci_msix_table_size(s1->dev);
337 g_assert_cmpuint(ret, ==, nvectors);
340 /* TODO test behavior before MSI-X is enabled */
342 /* ping vm2 -> vm1 on vector 0 */
343 if (msi) {
344 ret = qpci_msix_pending(s1->dev, 0);
345 g_assert_cmpuint(ret, ==, 0);
346 } else {
347 g_assert_cmpuint(in_reg(s1, INTRSTATUS), ==, 0);
349 out_reg(s2, DOORBELL, vm1 << 16);
350 do {
351 g_usleep(10000);
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 on vector 1 */
357 global_qtest = s2->qtest;
358 if (msi) {
359 ret = qpci_msix_pending(s2->dev, 1);
360 g_assert_cmpuint(ret, ==, 0);
361 } else {
362 g_assert_cmpuint(in_reg(s2, INTRSTATUS), ==, 0);
364 out_reg(s1, DOORBELL, vm2 << 16 | 1);
365 do {
366 g_usleep(10000);
367 ret = msi ? qpci_msix_pending(s2->dev, 1) : in_reg(s2, INTRSTATUS);
368 } while (ret == 0 && g_get_monotonic_time() < end_time);
369 g_assert_cmpuint(ret, !=, 0);
371 cleanup_vm(s2);
372 cleanup_vm(s1);
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)
399 gchar *opts;
401 qtest_start("");
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);
408 qtest_end();
409 g_free(opts);
412 static void test_ivshmem_memdev(void)
414 IVState state;
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-plain,memdev=mb1", false);
420 cleanup_vm(&state);
423 static void cleanup(void)
425 if (tmpshmem) {
426 munmap(tmpshmem, TMPSHMSIZE);
427 tmpshmem = NULL;
430 if (tmpshm) {
431 shm_unlink(tmpshm);
432 g_free(tmpshm);
433 tmpshm = NULL;
436 if (tmpserver) {
437 g_unlink(tmpserver);
438 g_free(tmpserver);
439 tmpserver = NULL;
442 if (tmpdir) {
443 g_rmdir(tmpdir);
444 tmpdir = NULL;
448 static void abrt_handler(void *data)
450 cleanup();
453 static gchar *mktempshm(int size, int *fd)
455 while (true) {
456 gchar *name;
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);
461 if (*fd > 0) {
462 g_assert(ftruncate(*fd, size) == 0);
463 return name;
466 g_free(name);
468 if (errno != EEXIST) {
469 perror("shm_open");
470 return NULL;
475 int main(int argc, char **argv)
477 int ret, fd;
478 gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
480 #if !GLIB_CHECK_VERSION(2, 31, 0)
481 if (!g_thread_supported()) {
482 g_thread_init(NULL);
484 #endif
486 g_test_init(&argc, &argv, NULL);
488 qtest_add_abrt_handler(abrt_handler, NULL);
489 /* shm */
490 tmpshm = mktempshm(TMPSHMSIZE, &fd);
491 if (!tmpshm) {
492 return 0;
494 tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
495 g_assert(tmpshmem != MAP_FAILED);
496 /* server */
497 if (mkdtemp(dir) == NULL) {
498 g_error("mkdtemp: %s", g_strerror(errno));
500 tmpdir = dir;
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);
506 if (g_test_slow()) {
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);
512 ret = g_test_run();
514 cleanup();
516 return ret;