]> git.proxmox.com Git - mirror_qemu.git/blob - hw/ivshmem.c
ivshmem: use EventNotifier and memory API
[mirror_qemu.git] / hw / ivshmem.c
1 /*
2 * Inter-VM Shared Memory PCI device.
3 *
4 * Author:
5 * Cam Macdonell <cam@cs.ualberta.ca>
6 *
7 * Based On: cirrus_vga.c
8 * Copyright (c) 2004 Fabrice Bellard
9 * Copyright (c) 2004 Makoto Suzuki (suzu)
10 *
11 * and rtl8139.c
12 * Copyright (c) 2006 Igor Kovalenko
13 *
14 * This code is licensed under the GNU GPL v2.
15 *
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
18 */
19 #include "hw.h"
20 #include "pc.h"
21 #include "pci.h"
22 #include "msix.h"
23 #include "kvm.h"
24 #include "migration.h"
25 #include "qerror.h"
26 #include "event_notifier.h"
27
28 #include <sys/mman.h>
29 #include <sys/types.h>
30
31 #define IVSHMEM_IOEVENTFD 0
32 #define IVSHMEM_MSI 1
33
34 #define IVSHMEM_PEER 0
35 #define IVSHMEM_MASTER 1
36
37 #define IVSHMEM_REG_BAR_SIZE 0x100
38
39 //#define DEBUG_IVSHMEM
40 #ifdef DEBUG_IVSHMEM
41 #define IVSHMEM_DPRINTF(fmt, ...) \
42 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
43 #else
44 #define IVSHMEM_DPRINTF(fmt, ...)
45 #endif
46
47 typedef struct Peer {
48 int nb_eventfds;
49 EventNotifier *eventfds;
50 } Peer;
51
52 typedef struct EventfdEntry {
53 PCIDevice *pdev;
54 int vector;
55 } EventfdEntry;
56
57 typedef struct IVShmemState {
58 PCIDevice dev;
59 uint32_t intrmask;
60 uint32_t intrstatus;
61 uint32_t doorbell;
62
63 CharDriverState **eventfd_chr;
64 CharDriverState *server_chr;
65 MemoryRegion ivshmem_mmio;
66
67 /* We might need to register the BAR before we actually have the memory.
68 * So prepare a container MemoryRegion for the BAR immediately and
69 * add a subregion when we have the memory.
70 */
71 MemoryRegion bar;
72 MemoryRegion ivshmem;
73 MemoryRegion msix_bar;
74 uint64_t ivshmem_size; /* size of shared memory region */
75 int shm_fd; /* shared memory file descriptor */
76
77 Peer *peers;
78 int nb_peers; /* how many guests we have space for */
79 int max_peer; /* maximum numbered peer */
80
81 int vm_id;
82 uint32_t vectors;
83 uint32_t features;
84 EventfdEntry *eventfd_table;
85
86 Error *migration_blocker;
87
88 char * shmobj;
89 char * sizearg;
90 char * role;
91 int role_val; /* scalar to avoid multiple string comparisons */
92 } IVShmemState;
93
94 /* registers for the Inter-VM shared memory device */
95 enum ivshmem_registers {
96 INTRMASK = 0,
97 INTRSTATUS = 4,
98 IVPOSITION = 8,
99 DOORBELL = 12,
100 };
101
102 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
103 unsigned int feature) {
104 return (ivs->features & (1 << feature));
105 }
106
107 static inline bool is_power_of_two(uint64_t x) {
108 return (x & (x - 1)) == 0;
109 }
110
111 /* accessing registers - based on rtl8139 */
112 static void ivshmem_update_irq(IVShmemState *s, int val)
113 {
114 int isr;
115 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
116
117 /* don't print ISR resets */
118 if (isr) {
119 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
120 isr ? 1 : 0, s->intrstatus, s->intrmask);
121 }
122
123 qemu_set_irq(s->dev.irq[0], (isr != 0));
124 }
125
126 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
127 {
128 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
129
130 s->intrmask = val;
131
132 ivshmem_update_irq(s, val);
133 }
134
135 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
136 {
137 uint32_t ret = s->intrmask;
138
139 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
140
141 return ret;
142 }
143
144 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
145 {
146 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
147
148 s->intrstatus = val;
149
150 ivshmem_update_irq(s, val);
151 return;
152 }
153
154 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
155 {
156 uint32_t ret = s->intrstatus;
157
158 /* reading ISR clears all interrupts */
159 s->intrstatus = 0;
160
161 ivshmem_update_irq(s, 0);
162
163 return ret;
164 }
165
166 static void ivshmem_io_write(void *opaque, target_phys_addr_t addr,
167 uint64_t val, unsigned size)
168 {
169 IVShmemState *s = opaque;
170
171 uint16_t dest = val >> 16;
172 uint16_t vector = val & 0xff;
173
174 addr &= 0xfc;
175
176 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
177 switch (addr)
178 {
179 case INTRMASK:
180 ivshmem_IntrMask_write(s, val);
181 break;
182
183 case INTRSTATUS:
184 ivshmem_IntrStatus_write(s, val);
185 break;
186
187 case DOORBELL:
188 /* check that dest VM ID is reasonable */
189 if (dest > s->max_peer) {
190 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
191 break;
192 }
193
194 /* check doorbell range */
195 if (vector < s->peers[dest].nb_eventfds) {
196 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
197 event_notifier_set(&s->peers[dest].eventfds[vector]);
198 }
199 break;
200 default:
201 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
202 }
203 }
204
205 static uint64_t ivshmem_io_read(void *opaque, target_phys_addr_t addr,
206 unsigned size)
207 {
208
209 IVShmemState *s = opaque;
210 uint32_t ret;
211
212 switch (addr)
213 {
214 case INTRMASK:
215 ret = ivshmem_IntrMask_read(s);
216 break;
217
218 case INTRSTATUS:
219 ret = ivshmem_IntrStatus_read(s);
220 break;
221
222 case IVPOSITION:
223 /* return my VM ID if the memory is mapped */
224 if (s->shm_fd > 0) {
225 ret = s->vm_id;
226 } else {
227 ret = -1;
228 }
229 break;
230
231 default:
232 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
233 ret = 0;
234 }
235
236 return ret;
237 }
238
239 static const MemoryRegionOps ivshmem_mmio_ops = {
240 .read = ivshmem_io_read,
241 .write = ivshmem_io_write,
242 .endianness = DEVICE_NATIVE_ENDIAN,
243 .impl = {
244 .min_access_size = 4,
245 .max_access_size = 4,
246 },
247 };
248
249 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
250 {
251 IVShmemState *s = opaque;
252
253 ivshmem_IntrStatus_write(s, *buf);
254
255 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
256 }
257
258 static int ivshmem_can_receive(void * opaque)
259 {
260 return 8;
261 }
262
263 static void ivshmem_event(void *opaque, int event)
264 {
265 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
266 }
267
268 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
269
270 EventfdEntry *entry = opaque;
271 PCIDevice *pdev = entry->pdev;
272
273 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
274 msix_notify(pdev, entry->vector);
275 }
276
277 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
278 int vector)
279 {
280 /* create a event character device based on the passed eventfd */
281 IVShmemState *s = opaque;
282 CharDriverState * chr;
283 int eventfd = event_notifier_get_fd(n);
284
285 chr = qemu_chr_open_eventfd(eventfd);
286
287 if (chr == NULL) {
288 fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
289 exit(-1);
290 }
291
292 /* if MSI is supported we need multiple interrupts */
293 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
294 s->eventfd_table[vector].pdev = &s->dev;
295 s->eventfd_table[vector].vector = vector;
296
297 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
298 ivshmem_event, &s->eventfd_table[vector]);
299 } else {
300 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
301 ivshmem_event, s);
302 }
303
304 return chr;
305
306 }
307
308 static int check_shm_size(IVShmemState *s, int fd) {
309 /* check that the guest isn't going to try and map more memory than the
310 * the object has allocated return -1 to indicate error */
311
312 struct stat buf;
313
314 fstat(fd, &buf);
315
316 if (s->ivshmem_size > buf.st_size) {
317 fprintf(stderr,
318 "IVSHMEM ERROR: Requested memory size greater"
319 " than shared object size (%" PRIu64 " > %" PRIu64")\n",
320 s->ivshmem_size, (uint64_t)buf.st_size);
321 return -1;
322 } else {
323 return 0;
324 }
325 }
326
327 /* create the shared memory BAR when we are not using the server, so we can
328 * create the BAR and map the memory immediately */
329 static void create_shared_memory_BAR(IVShmemState *s, int fd) {
330
331 void * ptr;
332
333 s->shm_fd = fd;
334
335 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
336
337 memory_region_init_ram_ptr(&s->ivshmem, "ivshmem.bar2",
338 s->ivshmem_size, ptr);
339 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
340 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
341
342 /* region for shared memory */
343 pci_register_bar(&s->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
344 }
345
346 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
347 {
348 memory_region_add_eventfd(&s->ivshmem_mmio,
349 DOORBELL,
350 4,
351 true,
352 (posn << 16) | i,
353 event_notifier_get_fd(&s->peers[posn].eventfds[i]));
354 }
355
356 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
357 {
358 memory_region_del_eventfd(&s->ivshmem_mmio,
359 DOORBELL,
360 4,
361 true,
362 (posn << 16) | i,
363 event_notifier_get_fd(&s->peers[posn].eventfds[i]));
364 }
365
366 static void close_guest_eventfds(IVShmemState *s, int posn)
367 {
368 int i, guest_curr_max;
369
370 guest_curr_max = s->peers[posn].nb_eventfds;
371
372 for (i = 0; i < guest_curr_max; i++) {
373 ivshmem_del_eventfd(s, posn, i);
374 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
375 }
376
377 g_free(s->peers[posn].eventfds);
378 s->peers[posn].nb_eventfds = 0;
379 }
380
381 static void setup_ioeventfds(IVShmemState *s) {
382
383 int i, j;
384
385 for (i = 0; i <= s->max_peer; i++) {
386 for (j = 0; j < s->peers[i].nb_eventfds; j++) {
387 ivshmem_add_eventfd(s, i, j);
388 }
389 }
390 }
391
392 /* this function increase the dynamic storage need to store data about other
393 * guests */
394 static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
395
396 int j, old_nb_alloc;
397
398 old_nb_alloc = s->nb_peers;
399
400 while (new_min_size >= s->nb_peers)
401 s->nb_peers = s->nb_peers * 2;
402
403 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
404 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
405
406 /* zero out new pointers */
407 for (j = old_nb_alloc; j < s->nb_peers; j++) {
408 s->peers[j].eventfds = NULL;
409 s->peers[j].nb_eventfds = 0;
410 }
411 }
412
413 static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
414 {
415 IVShmemState *s = opaque;
416 int incoming_fd, tmp_fd;
417 int guest_max_eventfd;
418 long incoming_posn;
419
420 memcpy(&incoming_posn, buf, sizeof(long));
421 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
422 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
423 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
424
425 /* make sure we have enough space for this guest */
426 if (incoming_posn >= s->nb_peers) {
427 increase_dynamic_storage(s, incoming_posn);
428 }
429
430 if (tmp_fd == -1) {
431 /* if posn is positive and unseen before then this is our posn*/
432 if ((incoming_posn >= 0) &&
433 (s->peers[incoming_posn].eventfds == NULL)) {
434 /* receive our posn */
435 s->vm_id = incoming_posn;
436 return;
437 } else {
438 /* otherwise an fd == -1 means an existing guest has gone away */
439 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
440 close_guest_eventfds(s, incoming_posn);
441 return;
442 }
443 }
444
445 /* because of the implementation of get_msgfd, we need a dup */
446 incoming_fd = dup(tmp_fd);
447
448 if (incoming_fd == -1) {
449 fprintf(stderr, "could not allocate file descriptor %s\n",
450 strerror(errno));
451 return;
452 }
453
454 /* if the position is -1, then it's shared memory region fd */
455 if (incoming_posn == -1) {
456
457 void * map_ptr;
458
459 s->max_peer = 0;
460
461 if (check_shm_size(s, incoming_fd) == -1) {
462 exit(-1);
463 }
464
465 /* mmap the region and map into the BAR2 */
466 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
467 incoming_fd, 0);
468 memory_region_init_ram_ptr(&s->ivshmem,
469 "ivshmem.bar2", s->ivshmem_size, map_ptr);
470 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
471
472 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
473 s->ivshmem_offset, s->ivshmem_size);
474
475 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
476
477 /* only store the fd if it is successfully mapped */
478 s->shm_fd = incoming_fd;
479
480 return;
481 }
482
483 /* each guest has an array of eventfds, and we keep track of how many
484 * guests for each VM */
485 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
486
487 if (guest_max_eventfd == 0) {
488 /* one eventfd per MSI vector */
489 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
490 }
491
492 /* this is an eventfd for a particular guest VM */
493 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
494 guest_max_eventfd, incoming_fd);
495 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
496 incoming_fd);
497
498 /* increment count for particular guest */
499 s->peers[incoming_posn].nb_eventfds++;
500
501 /* keep track of the maximum VM ID */
502 if (incoming_posn > s->max_peer) {
503 s->max_peer = incoming_posn;
504 }
505
506 if (incoming_posn == s->vm_id) {
507 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
508 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
509 guest_max_eventfd);
510 }
511
512 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
513 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
514 }
515
516 return;
517 }
518
519 /* Select the MSI-X vectors used by device.
520 * ivshmem maps events to vectors statically, so
521 * we just enable all vectors on init and after reset. */
522 static void ivshmem_use_msix(IVShmemState * s)
523 {
524 int i;
525
526 if (!msix_present(&s->dev)) {
527 return;
528 }
529
530 for (i = 0; i < s->vectors; i++) {
531 msix_vector_use(&s->dev, i);
532 }
533 }
534
535 static void ivshmem_reset(DeviceState *d)
536 {
537 IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
538
539 s->intrstatus = 0;
540 ivshmem_use_msix(s);
541 return;
542 }
543
544 static uint64_t ivshmem_get_size(IVShmemState * s) {
545
546 uint64_t value;
547 char *ptr;
548
549 value = strtoull(s->sizearg, &ptr, 10);
550 switch (*ptr) {
551 case 0: case 'M': case 'm':
552 value <<= 20;
553 break;
554 case 'G': case 'g':
555 value <<= 30;
556 break;
557 default:
558 fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
559 exit(1);
560 }
561
562 /* BARs must be a power of 2 */
563 if (!is_power_of_two(value)) {
564 fprintf(stderr, "ivshmem: size must be power of 2\n");
565 exit(1);
566 }
567
568 return value;
569 }
570
571 static void ivshmem_setup_msi(IVShmemState * s)
572 {
573 memory_region_init(&s->msix_bar, "ivshmem-msix", 4096);
574 if (!msix_init(&s->dev, s->vectors, &s->msix_bar, 1, 0)) {
575 pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
576 &s->msix_bar);
577 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
578 } else {
579 IVSHMEM_DPRINTF("msix initialization failed\n");
580 exit(1);
581 }
582
583 /* allocate QEMU char devices for receiving interrupts */
584 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
585
586 ivshmem_use_msix(s);
587 }
588
589 static void ivshmem_save(QEMUFile* f, void *opaque)
590 {
591 IVShmemState *proxy = opaque;
592
593 IVSHMEM_DPRINTF("ivshmem_save\n");
594 pci_device_save(&proxy->dev, f);
595
596 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
597 msix_save(&proxy->dev, f);
598 } else {
599 qemu_put_be32(f, proxy->intrstatus);
600 qemu_put_be32(f, proxy->intrmask);
601 }
602
603 }
604
605 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
606 {
607 IVSHMEM_DPRINTF("ivshmem_load\n");
608
609 IVShmemState *proxy = opaque;
610 int ret;
611
612 if (version_id > 0) {
613 return -EINVAL;
614 }
615
616 if (proxy->role_val == IVSHMEM_PEER) {
617 fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
618 return -EINVAL;
619 }
620
621 ret = pci_device_load(&proxy->dev, f);
622 if (ret) {
623 return ret;
624 }
625
626 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
627 msix_load(&proxy->dev, f);
628 ivshmem_use_msix(proxy);
629 } else {
630 proxy->intrstatus = qemu_get_be32(f);
631 proxy->intrmask = qemu_get_be32(f);
632 }
633
634 return 0;
635 }
636
637 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
638 uint32_t val, int len)
639 {
640 pci_default_write_config(pci_dev, address, val, len);
641 msix_write_config(pci_dev, address, val, len);
642 }
643
644 static int pci_ivshmem_init(PCIDevice *dev)
645 {
646 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
647 uint8_t *pci_conf;
648
649 if (s->sizearg == NULL)
650 s->ivshmem_size = 4 << 20; /* 4 MB default */
651 else {
652 s->ivshmem_size = ivshmem_get_size(s);
653 }
654
655 register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
656 dev);
657
658 /* IRQFD requires MSI */
659 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
660 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
661 fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
662 exit(1);
663 }
664
665 /* check that role is reasonable */
666 if (s->role) {
667 if (strncmp(s->role, "peer", 5) == 0) {
668 s->role_val = IVSHMEM_PEER;
669 } else if (strncmp(s->role, "master", 7) == 0) {
670 s->role_val = IVSHMEM_MASTER;
671 } else {
672 fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
673 exit(1);
674 }
675 } else {
676 s->role_val = IVSHMEM_MASTER; /* default */
677 }
678
679 if (s->role_val == IVSHMEM_PEER) {
680 error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION, "ivshmem", "peer mode");
681 migrate_add_blocker(s->migration_blocker);
682 }
683
684 pci_conf = s->dev.config;
685 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
686
687 pci_config_set_interrupt_pin(pci_conf, 1);
688
689 s->shm_fd = 0;
690
691 memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
692 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
693
694 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
695 setup_ioeventfds(s);
696 }
697
698 /* region for registers*/
699 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
700 &s->ivshmem_mmio);
701
702 memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
703
704 if ((s->server_chr != NULL) &&
705 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
706 /* if we get a UNIX socket as the parameter we will talk
707 * to the ivshmem server to receive the memory region */
708
709 if (s->shmobj != NULL) {
710 fprintf(stderr, "WARNING: do not specify both 'chardev' "
711 "and 'shm' with ivshmem\n");
712 }
713
714 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
715 s->server_chr->filename);
716
717 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
718 ivshmem_setup_msi(s);
719 }
720
721 /* we allocate enough space for 16 guests and grow as needed */
722 s->nb_peers = 16;
723 s->vm_id = -1;
724
725 /* allocate/initialize space for interrupt handling */
726 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
727
728 pci_register_bar(&s->dev, 2,
729 PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
730
731 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
732
733 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
734 ivshmem_event, s);
735 } else {
736 /* just map the file immediately, we're not using a server */
737 int fd;
738
739 if (s->shmobj == NULL) {
740 fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
741 }
742
743 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
744
745 /* try opening with O_EXCL and if it succeeds zero the memory
746 * by truncating to 0 */
747 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
748 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
749 /* truncate file to length PCI device's memory */
750 if (ftruncate(fd, s->ivshmem_size) != 0) {
751 fprintf(stderr, "ivshmem: could not truncate shared file\n");
752 }
753
754 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
755 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
756 fprintf(stderr, "ivshmem: could not open shared file\n");
757 exit(-1);
758
759 }
760
761 if (check_shm_size(s, fd) == -1) {
762 exit(-1);
763 }
764
765 create_shared_memory_BAR(s, fd);
766
767 }
768
769 s->dev.config_write = ivshmem_write_config;
770
771 return 0;
772 }
773
774 static int pci_ivshmem_uninit(PCIDevice *dev)
775 {
776 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
777
778 if (s->migration_blocker) {
779 migrate_del_blocker(s->migration_blocker);
780 error_free(s->migration_blocker);
781 }
782
783 memory_region_destroy(&s->ivshmem_mmio);
784 memory_region_del_subregion(&s->bar, &s->ivshmem);
785 vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
786 memory_region_destroy(&s->ivshmem);
787 memory_region_destroy(&s->bar);
788 unregister_savevm(&dev->qdev, "ivshmem", s);
789
790 return 0;
791 }
792
793 static Property ivshmem_properties[] = {
794 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
795 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
796 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
797 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
798 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
799 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
800 DEFINE_PROP_STRING("role", IVShmemState, role),
801 DEFINE_PROP_END_OF_LIST(),
802 };
803
804 static void ivshmem_class_init(ObjectClass *klass, void *data)
805 {
806 DeviceClass *dc = DEVICE_CLASS(klass);
807 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
808
809 k->init = pci_ivshmem_init;
810 k->exit = pci_ivshmem_uninit;
811 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
812 k->device_id = 0x1110;
813 k->class_id = PCI_CLASS_MEMORY_RAM;
814 dc->reset = ivshmem_reset;
815 dc->props = ivshmem_properties;
816 }
817
818 static TypeInfo ivshmem_info = {
819 .name = "ivshmem",
820 .parent = TYPE_PCI_DEVICE,
821 .instance_size = sizeof(IVShmemState),
822 .class_init = ivshmem_class_init,
823 };
824
825 static void ivshmem_register_types(void)
826 {
827 type_register_static(&ivshmem_info);
828 }
829
830 type_init(ivshmem_register_types)