2 * Inter-VM Shared Memory PCI device.
7 * Based On: cirrus_vga.c
8 * Copyright (c) 2004 Fabrice Bellard
9 * Copyright (c) 2004 Makoto Suzuki (suzu)
12 * Copyright (c) 2006 Igor Kovalenko
14 * This code is licensed under the GNU GPL v2.
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.
20 #include "hw/i386/pc.h"
21 #include "hw/pci/pci.h"
22 #include "hw/pci/msi.h"
23 #include "hw/pci/msix.h"
24 #include "sysemu/kvm.h"
25 #include "migration/migration.h"
26 #include "qemu/error-report.h"
27 #include "qemu/event_notifier.h"
28 #include "qemu/fifo8.h"
29 #include "sysemu/char.h"
30 #include "sysemu/hostmem.h"
31 #include "qapi/visitor.h"
33 #include "hw/misc/ivshmem.h"
36 #include <sys/types.h>
39 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
40 #define PCI_DEVICE_ID_IVSHMEM 0x1110
42 #define IVSHMEM_MAX_PEERS G_MAXUINT16
43 #define IVSHMEM_IOEVENTFD 0
46 #define IVSHMEM_PEER 0
47 #define IVSHMEM_MASTER 1
49 #define IVSHMEM_REG_BAR_SIZE 0x100
51 //#define DEBUG_IVSHMEM
53 #define IVSHMEM_DPRINTF(fmt, ...) \
54 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
56 #define IVSHMEM_DPRINTF(fmt, ...)
59 #define TYPE_IVSHMEM "ivshmem"
60 #define IVSHMEM(obj) \
61 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
63 #define IVSHMEM_MEMDEV_PROP "memdev"
67 EventNotifier *eventfds;
70 typedef struct MSIVector {
75 typedef struct IVShmemState {
80 HostMemoryBackend *hostmem;
84 CharDriverState **eventfd_chr;
85 CharDriverState *server_chr;
87 MemoryRegion ivshmem_mmio;
89 /* We might need to register the BAR before we actually have the memory.
90 * So prepare a container MemoryRegion for the BAR immediately and
91 * add a subregion when we have the memory.
95 uint64_t ivshmem_size; /* size of shared memory region */
96 uint32_t ivshmem_64bit;
99 int nb_peers; /* how many peers we have space for */
104 MSIVector *msi_vectors;
106 Error *migration_blocker;
111 int role_val; /* scalar to avoid multiple string comparisons */
114 /* registers for the Inter-VM shared memory device */
115 enum ivshmem_registers {
122 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
123 unsigned int feature) {
124 return (ivs->features & (1 << feature));
127 /* accessing registers - based on rtl8139 */
128 static void ivshmem_update_irq(IVShmemState *s)
130 PCIDevice *d = PCI_DEVICE(s);
132 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
134 /* don't print ISR resets */
136 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
137 isr ? 1 : 0, s->intrstatus, s->intrmask);
140 pci_set_irq(d, (isr != 0));
143 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
145 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
149 ivshmem_update_irq(s);
152 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
154 uint32_t ret = s->intrmask;
156 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
161 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
163 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
167 ivshmem_update_irq(s);
170 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
172 uint32_t ret = s->intrstatus;
174 /* reading ISR clears all interrupts */
177 ivshmem_update_irq(s);
182 static void ivshmem_io_write(void *opaque, hwaddr addr,
183 uint64_t val, unsigned size)
185 IVShmemState *s = opaque;
187 uint16_t dest = val >> 16;
188 uint16_t vector = val & 0xff;
192 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
196 ivshmem_IntrMask_write(s, val);
200 ivshmem_IntrStatus_write(s, val);
204 /* check that dest VM ID is reasonable */
205 if (dest >= s->nb_peers) {
206 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
210 /* check doorbell range */
211 if (vector < s->peers[dest].nb_eventfds) {
212 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
213 event_notifier_set(&s->peers[dest].eventfds[vector]);
215 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
220 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx "\n", addr);
224 static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
228 IVShmemState *s = opaque;
234 ret = ivshmem_IntrMask_read(s);
238 ret = ivshmem_IntrStatus_read(s);
242 /* return my VM ID if the memory is mapped */
243 if (memory_region_is_mapped(&s->ivshmem)) {
251 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
258 static const MemoryRegionOps ivshmem_mmio_ops = {
259 .read = ivshmem_io_read,
260 .write = ivshmem_io_write,
261 .endianness = DEVICE_NATIVE_ENDIAN,
263 .min_access_size = 4,
264 .max_access_size = 4,
268 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
270 IVShmemState *s = opaque;
272 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x size: %d\n", *buf, size);
274 ivshmem_IntrStatus_write(s, *buf);
277 static int ivshmem_can_receive(void * opaque)
282 static void ivshmem_event(void *opaque, int event)
284 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
287 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
289 MSIVector *entry = opaque;
290 PCIDevice *pdev = entry->pdev;
291 IVShmemState *s = IVSHMEM(pdev);
292 int vector = entry - s->msi_vectors;
294 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, vector);
295 msix_notify(pdev, vector);
298 static int ivshmem_vector_unmask(PCIDevice *dev, unsigned vector,
301 IVShmemState *s = IVSHMEM(dev);
302 EventNotifier *n = &s->peers[s->vm_id].eventfds[vector];
303 MSIVector *v = &s->msi_vectors[vector];
306 IVSHMEM_DPRINTF("vector unmask %p %d\n", dev, vector);
308 ret = kvm_irqchip_update_msi_route(kvm_state, v->virq, msg, dev);
313 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, v->virq);
316 static void ivshmem_vector_mask(PCIDevice *dev, unsigned vector)
318 IVShmemState *s = IVSHMEM(dev);
319 EventNotifier *n = &s->peers[s->vm_id].eventfds[vector];
322 IVSHMEM_DPRINTF("vector mask %p %d\n", dev, vector);
324 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n,
325 s->msi_vectors[vector].virq);
327 error_report("remove_irqfd_notifier_gsi failed");
331 static void ivshmem_vector_poll(PCIDevice *dev,
332 unsigned int vector_start,
333 unsigned int vector_end)
335 IVShmemState *s = IVSHMEM(dev);
338 IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev, vector_start, vector_end);
340 vector_end = MIN(vector_end, s->vectors);
342 for (vector = vector_start; vector < vector_end; vector++) {
343 EventNotifier *notifier = &s->peers[s->vm_id].eventfds[vector];
345 if (!msix_is_masked(dev, vector)) {
349 if (event_notifier_test_and_clear(notifier)) {
350 msix_set_pending(dev, vector);
355 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
358 /* create a event character device based on the passed eventfd */
359 IVShmemState *s = opaque;
360 PCIDevice *pdev = PCI_DEVICE(s);
361 int eventfd = event_notifier_get_fd(n);
362 CharDriverState *chr;
364 s->msi_vectors[vector].pdev = pdev;
366 chr = qemu_chr_open_eventfd(eventfd);
369 error_report("creating chardriver for eventfd %d failed", eventfd);
372 qemu_chr_fe_claim_no_fail(chr);
374 /* if MSI is supported we need multiple interrupts */
375 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
376 s->msi_vectors[vector].pdev = PCI_DEVICE(s);
378 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
379 ivshmem_event, &s->msi_vectors[vector]);
381 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
389 static int check_shm_size(IVShmemState *s, int fd, Error **errp)
391 /* check that the guest isn't going to try and map more memory than the
392 * the object has allocated return -1 to indicate error */
396 if (fstat(fd, &buf) < 0) {
397 error_setg(errp, "exiting: fstat on fd %d failed: %s",
398 fd, strerror(errno));
402 if (s->ivshmem_size > buf.st_size) {
403 error_setg(errp, "Requested memory size greater"
404 " than shared object size (%" PRIu64 " > %" PRIu64")",
405 s->ivshmem_size, (uint64_t)buf.st_size);
412 /* create the shared memory BAR when we are not using the server, so we can
413 * create the BAR and map the memory immediately */
414 static int create_shared_memory_BAR(IVShmemState *s, int fd, uint8_t attr,
419 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
420 if (ptr == MAP_FAILED) {
421 error_setg_errno(errp, errno, "Failed to mmap shared memory");
425 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2",
426 s->ivshmem_size, ptr);
427 vmstate_register_ram(&s->ivshmem, DEVICE(s));
428 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
430 /* region for shared memory */
431 pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
436 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
438 memory_region_add_eventfd(&s->ivshmem_mmio,
443 &s->peers[posn].eventfds[i]);
446 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
448 memory_region_del_eventfd(&s->ivshmem_mmio,
453 &s->peers[posn].eventfds[i]);
456 static void close_peer_eventfds(IVShmemState *s, int posn)
460 if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
463 if (posn < 0 || posn >= s->nb_peers) {
464 error_report("invalid peer %d", posn);
468 n = s->peers[posn].nb_eventfds;
470 memory_region_transaction_begin();
471 for (i = 0; i < n; i++) {
472 ivshmem_del_eventfd(s, posn, i);
474 memory_region_transaction_commit();
475 for (i = 0; i < n; i++) {
476 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
479 g_free(s->peers[posn].eventfds);
480 s->peers[posn].nb_eventfds = 0;
483 /* this function increase the dynamic storage need to store data about other
485 static int resize_peers(IVShmemState *s, int new_min_size)
490 /* limit number of max peers */
491 if (new_min_size <= 0 || new_min_size > IVSHMEM_MAX_PEERS) {
494 if (new_min_size <= s->nb_peers) {
498 old_size = s->nb_peers;
499 s->nb_peers = new_min_size;
501 IVSHMEM_DPRINTF("bumping storage to %d peers\n", s->nb_peers);
503 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
505 for (j = old_size; j < s->nb_peers; j++) {
506 s->peers[j].eventfds = g_new0(EventNotifier, s->vectors);
507 s->peers[j].nb_eventfds = 0;
513 static bool fifo_update_and_get(IVShmemState *s, const uint8_t *buf, int size,
514 void *data, size_t len)
519 assert(len <= sizeof(long)); /* limitation of the fifo */
520 if (fifo8_is_empty(&s->incoming_fifo) && size == len) {
521 memcpy(data, buf, size);
525 IVSHMEM_DPRINTF("short read of %d bytes\n", size);
527 num = MIN(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo));
528 fifo8_push_all(&s->incoming_fifo, buf, num);
530 if (fifo8_num_used(&s->incoming_fifo) < len) {
537 p = fifo8_pop_buf(&s->incoming_fifo, len, &num);
540 memcpy(data, p, len);
543 fifo8_push_all(&s->incoming_fifo, buf, size);
549 static int ivshmem_add_kvm_msi_virq(IVShmemState *s, int vector)
551 PCIDevice *pdev = PCI_DEVICE(s);
552 MSIMessage msg = msix_get_message(pdev, vector);
555 IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector);
557 if (s->msi_vectors[vector].pdev != NULL) {
561 ret = kvm_irqchip_add_msi_route(kvm_state, msg, pdev);
563 error_report("ivshmem: kvm_irqchip_add_msi_route failed");
567 s->msi_vectors[vector].virq = ret;
568 s->msi_vectors[vector].pdev = pdev;
573 static void setup_interrupt(IVShmemState *s, int vector)
575 EventNotifier *n = &s->peers[s->vm_id].eventfds[vector];
576 bool with_irqfd = kvm_msi_via_irqfd_enabled() &&
577 ivshmem_has_feature(s, IVSHMEM_MSI);
578 PCIDevice *pdev = PCI_DEVICE(s);
580 IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector);
583 IVSHMEM_DPRINTF("with eventfd");
584 s->eventfd_chr[vector] = create_eventfd_chr_device(s, n, vector);
585 } else if (msix_enabled(pdev)) {
586 IVSHMEM_DPRINTF("with irqfd");
587 if (ivshmem_add_kvm_msi_virq(s, vector) < 0) {
591 if (!msix_is_masked(pdev, vector)) {
592 kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL,
593 s->msi_vectors[vector].virq);
596 /* it will be delayed until msix is enabled, in write_config */
597 IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled");
601 static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
603 IVShmemState *s = opaque;
610 if (!fifo_update_and_get(s, buf, size,
611 &incoming_posn, sizeof(incoming_posn))) {
615 if (incoming_posn < -1) {
616 IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn);
620 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
621 incoming_fd = qemu_chr_fe_get_msgfd(s->server_chr);
622 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, incoming_fd);
624 /* make sure we have enough space for this peer */
625 if (incoming_posn >= s->nb_peers) {
626 if (resize_peers(s, incoming_posn + 1) < 0) {
627 error_report("failed to resize peers array");
628 if (incoming_fd != -1) {
635 peer = &s->peers[incoming_posn];
637 if (incoming_fd == -1) {
638 /* if posn is positive and unseen before then this is our posn*/
639 if (incoming_posn >= 0 && s->vm_id == -1) {
640 /* receive our posn */
641 s->vm_id = incoming_posn;
643 /* otherwise an fd == -1 means an existing peer has gone away */
644 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
645 close_peer_eventfds(s, incoming_posn);
650 /* if the position is -1, then it's shared memory region fd */
651 if (incoming_posn == -1) {
654 if (memory_region_is_mapped(&s->ivshmem)) {
655 error_report("shm already initialized");
660 if (check_shm_size(s, incoming_fd, &err) == -1) {
661 error_report_err(err);
666 /* mmap the region and map into the BAR2 */
667 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
669 if (map_ptr == MAP_FAILED) {
670 error_report("Failed to mmap shared memory %s", strerror(errno));
674 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
675 "ivshmem.bar2", s->ivshmem_size, map_ptr);
676 vmstate_register_ram(&s->ivshmem, DEVICE(s));
678 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",
679 map_ptr, s->ivshmem_size);
681 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
687 /* each peer has an associated array of eventfds, and we keep
688 * track of how many eventfds received so far */
689 /* get a new eventfd: */
690 if (peer->nb_eventfds >= s->vectors) {
691 error_report("Too many eventfd received, device has %d vectors",
697 new_eventfd = peer->nb_eventfds++;
699 /* this is an eventfd for a particular peer VM */
700 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
701 new_eventfd, incoming_fd);
702 event_notifier_init_fd(&peer->eventfds[new_eventfd], incoming_fd);
703 fcntl_setfl(incoming_fd, O_NONBLOCK); /* msix/irqfd poll non block */
705 if (incoming_posn == s->vm_id) {
706 setup_interrupt(s, new_eventfd);
709 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
710 ivshmem_add_eventfd(s, incoming_posn, new_eventfd);
714 static void ivshmem_check_version(void *opaque, const uint8_t * buf, int size)
716 IVShmemState *s = opaque;
720 if (!fifo_update_and_get(s, buf, size,
721 &version, sizeof(version))) {
725 tmp = qemu_chr_fe_get_msgfd(s->server_chr);
726 if (tmp != -1 || version != IVSHMEM_PROTOCOL_VERSION) {
727 fprintf(stderr, "incompatible version, you are connecting to a ivshmem-"
728 "server using a different protocol please check your setup\n");
729 qemu_chr_delete(s->server_chr);
730 s->server_chr = NULL;
734 IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
735 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
739 /* Select the MSI-X vectors used by device.
740 * ivshmem maps events to vectors statically, so
741 * we just enable all vectors on init and after reset. */
742 static void ivshmem_use_msix(IVShmemState * s)
744 PCIDevice *d = PCI_DEVICE(s);
747 IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__, msix_present(d));
748 if (!msix_present(d)) {
752 for (i = 0; i < s->vectors; i++) {
753 msix_vector_use(d, i);
757 static void ivshmem_reset(DeviceState *d)
759 IVShmemState *s = IVSHMEM(d);
766 static int ivshmem_setup_msi(IVShmemState * s)
768 if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
772 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
774 /* allocate QEMU char devices for receiving interrupts */
775 s->msi_vectors = g_malloc0(s->vectors * sizeof(MSIVector));
781 static void ivshmem_enable_irqfd(IVShmemState *s)
783 PCIDevice *pdev = PCI_DEVICE(s);
786 for (i = 0; i < s->peers[s->vm_id].nb_eventfds; i++) {
787 ivshmem_add_kvm_msi_virq(s, i);
790 if (msix_set_vector_notifiers(pdev,
791 ivshmem_vector_unmask,
793 ivshmem_vector_poll)) {
794 error_report("ivshmem: msix_set_vector_notifiers failed");
798 static void ivshmem_remove_kvm_msi_virq(IVShmemState *s, int vector)
800 IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector);
802 if (s->msi_vectors[vector].pdev == NULL) {
806 /* it was cleaned when masked in the frontend. */
807 kvm_irqchip_release_virq(kvm_state, s->msi_vectors[vector].virq);
809 s->msi_vectors[vector].pdev = NULL;
812 static void ivshmem_disable_irqfd(IVShmemState *s)
814 PCIDevice *pdev = PCI_DEVICE(s);
817 for (i = 0; i < s->peers[s->vm_id].nb_eventfds; i++) {
818 ivshmem_remove_kvm_msi_virq(s, i);
821 msix_unset_vector_notifiers(pdev);
824 static void ivshmem_write_config(PCIDevice *pdev, uint32_t address,
825 uint32_t val, int len)
827 IVShmemState *s = IVSHMEM(pdev);
828 int is_enabled, was_enabled = msix_enabled(pdev);
830 pci_default_write_config(pdev, address, val, len);
831 is_enabled = msix_enabled(pdev);
833 if (kvm_msi_via_irqfd_enabled() && s->vm_id != -1) {
834 if (!was_enabled && is_enabled) {
835 ivshmem_enable_irqfd(s);
836 } else if (was_enabled && !is_enabled) {
837 ivshmem_disable_irqfd(s);
842 static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
844 IVShmemState *s = IVSHMEM(dev);
846 uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
847 PCI_BASE_ADDRESS_MEM_PREFETCH;
849 if (!!s->server_chr + !!s->shmobj + !!s->hostmem != 1) {
850 error_setg(errp, "You must specify either a shmobj, a chardev"
859 g_warning("size argument ignored with hostmem");
862 mr = host_memory_backend_get_memory(s->hostmem, errp);
863 s->ivshmem_size = memory_region_size(mr);
864 } else if (s->sizearg == NULL) {
865 s->ivshmem_size = 4 << 20; /* 4 MB default */
868 int64_t size = qemu_strtosz(s->sizearg, &end);
869 if (size < 0 || *end != '\0' || !is_power_of_2(size)) {
870 error_setg(errp, "Invalid size %s", s->sizearg);
873 s->ivshmem_size = size;
876 fifo8_create(&s->incoming_fifo, sizeof(long));
878 /* IRQFD requires MSI */
879 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
880 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
881 error_setg(errp, "ioeventfd/irqfd requires MSI");
885 /* check that role is reasonable */
887 if (strncmp(s->role, "peer", 5) == 0) {
888 s->role_val = IVSHMEM_PEER;
889 } else if (strncmp(s->role, "master", 7) == 0) {
890 s->role_val = IVSHMEM_MASTER;
892 error_setg(errp, "'role' must be 'peer' or 'master'");
896 s->role_val = IVSHMEM_MASTER; /* default */
899 if (s->role_val == IVSHMEM_PEER) {
900 error_setg(&s->migration_blocker,
901 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
902 migrate_add_blocker(s->migration_blocker);
905 pci_conf = dev->config;
906 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
908 pci_config_set_interrupt_pin(pci_conf, 1);
910 memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
911 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
913 /* region for registers*/
914 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
917 memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
918 if (s->ivshmem_64bit) {
919 attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
922 if (s->hostmem != NULL) {
925 IVSHMEM_DPRINTF("using hostmem\n");
927 mr = host_memory_backend_get_memory(MEMORY_BACKEND(s->hostmem), errp);
928 vmstate_register_ram(mr, DEVICE(s));
929 memory_region_add_subregion(&s->bar, 0, mr);
930 pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
931 } else if (s->server_chr != NULL) {
932 if (strncmp(s->server_chr->filename, "unix:", 5)) {
933 error_setg(errp, "chardev is not a unix client socket");
937 /* if we get a UNIX socket as the parameter we will talk
938 * to the ivshmem server to receive the memory region */
940 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
941 s->server_chr->filename);
943 if (ivshmem_has_feature(s, IVSHMEM_MSI) &&
944 ivshmem_setup_msi(s)) {
945 error_setg(errp, "msix initialization failed");
949 /* we allocate enough space for 16 peers and grow as needed */
953 pci_register_bar(dev, 2, attr, &s->bar);
955 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
957 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive,
958 ivshmem_check_version, ivshmem_event, s);
960 /* just map the file immediately, we're not using a server */
963 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
965 /* try opening with O_EXCL and if it succeeds zero the memory
966 * by truncating to 0 */
967 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
968 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
969 /* truncate file to length PCI device's memory */
970 if (ftruncate(fd, s->ivshmem_size) != 0) {
971 error_report("could not truncate shared file");
974 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
975 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
976 error_setg(errp, "could not open shared file");
980 if (check_shm_size(s, fd, errp) == -1) {
984 create_shared_memory_BAR(s, fd, attr, errp);
989 static void pci_ivshmem_exit(PCIDevice *dev)
991 IVShmemState *s = IVSHMEM(dev);
994 fifo8_destroy(&s->incoming_fifo);
996 if (s->migration_blocker) {
997 migrate_del_blocker(s->migration_blocker);
998 error_free(s->migration_blocker);
1001 if (memory_region_is_mapped(&s->ivshmem)) {
1003 void *addr = memory_region_get_ram_ptr(&s->ivshmem);
1005 if (munmap(addr, s->ivshmem_size) == -1) {
1006 error_report("Failed to munmap shared memory %s",
1011 vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
1012 memory_region_del_subregion(&s->bar, &s->ivshmem);
1015 if (s->eventfd_chr) {
1016 for (i = 0; i < s->vectors; i++) {
1017 if (s->eventfd_chr[i]) {
1018 qemu_chr_free(s->eventfd_chr[i]);
1021 g_free(s->eventfd_chr);
1025 for (i = 0; i < s->nb_peers; i++) {
1026 close_peer_eventfds(s, i);
1031 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
1032 msix_uninit_exclusive_bar(dev);
1035 g_free(s->msi_vectors);
1038 static bool test_msix(void *opaque, int version_id)
1040 IVShmemState *s = opaque;
1042 return ivshmem_has_feature(s, IVSHMEM_MSI);
1045 static bool test_no_msix(void *opaque, int version_id)
1047 return !test_msix(opaque, version_id);
1050 static int ivshmem_pre_load(void *opaque)
1052 IVShmemState *s = opaque;
1054 if (s->role_val == IVSHMEM_PEER) {
1055 error_report("'peer' devices are not migratable");
1062 static int ivshmem_post_load(void *opaque, int version_id)
1064 IVShmemState *s = opaque;
1066 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
1067 ivshmem_use_msix(s);
1073 static int ivshmem_load_old(QEMUFile *f, void *opaque, int version_id)
1075 IVShmemState *s = opaque;
1076 PCIDevice *pdev = PCI_DEVICE(s);
1079 IVSHMEM_DPRINTF("ivshmem_load_old\n");
1081 if (version_id != 0) {
1085 if (s->role_val == IVSHMEM_PEER) {
1086 error_report("'peer' devices are not migratable");
1090 ret = pci_device_load(pdev, f);
1095 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
1097 ivshmem_use_msix(s);
1099 s->intrstatus = qemu_get_be32(f);
1100 s->intrmask = qemu_get_be32(f);
1106 static const VMStateDescription ivshmem_vmsd = {
1109 .minimum_version_id = 1,
1110 .pre_load = ivshmem_pre_load,
1111 .post_load = ivshmem_post_load,
1112 .fields = (VMStateField[]) {
1113 VMSTATE_PCI_DEVICE(parent_obj, IVShmemState),
1115 VMSTATE_MSIX_TEST(parent_obj, IVShmemState, test_msix),
1116 VMSTATE_UINT32_TEST(intrstatus, IVShmemState, test_no_msix),
1117 VMSTATE_UINT32_TEST(intrmask, IVShmemState, test_no_msix),
1119 VMSTATE_END_OF_LIST()
1121 .load_state_old = ivshmem_load_old,
1122 .minimum_version_id_old = 0
1125 static Property ivshmem_properties[] = {
1126 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
1127 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
1128 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
1129 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
1130 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
1131 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
1132 DEFINE_PROP_STRING("role", IVShmemState, role),
1133 DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
1134 DEFINE_PROP_END_OF_LIST(),
1137 static void ivshmem_class_init(ObjectClass *klass, void *data)
1139 DeviceClass *dc = DEVICE_CLASS(klass);
1140 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1142 k->realize = pci_ivshmem_realize;
1143 k->exit = pci_ivshmem_exit;
1144 k->config_write = ivshmem_write_config;
1145 k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
1146 k->device_id = PCI_DEVICE_ID_IVSHMEM;
1147 k->class_id = PCI_CLASS_MEMORY_RAM;
1148 dc->reset = ivshmem_reset;
1149 dc->props = ivshmem_properties;
1150 dc->vmsd = &ivshmem_vmsd;
1151 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1152 dc->desc = "Inter-VM shared memory";
1155 static void ivshmem_check_memdev_is_busy(Object *obj, const char *name,
1156 Object *val, Error **errp)
1160 mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
1161 if (memory_region_is_mapped(mr)) {
1162 char *path = object_get_canonical_path_component(val);
1163 error_setg(errp, "can't use already busy memdev: %s", path);
1166 qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
1170 static void ivshmem_init(Object *obj)
1172 IVShmemState *s = IVSHMEM(obj);
1174 object_property_add_link(obj, IVSHMEM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
1175 (Object **)&s->hostmem,
1176 ivshmem_check_memdev_is_busy,
1177 OBJ_PROP_LINK_UNREF_ON_RELEASE,
1181 static const TypeInfo ivshmem_info = {
1182 .name = TYPE_IVSHMEM,
1183 .parent = TYPE_PCI_DEVICE,
1184 .instance_size = sizeof(IVShmemState),
1185 .instance_init = ivshmem_init,
1186 .class_init = ivshmem_class_init,
1189 static void ivshmem_register_types(void)
1191 type_register_static(&ivshmem_info);
1194 type_init(ivshmem_register_types)