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.
24 #include "migration.h"
28 #include <sys/types.h>
30 #define IVSHMEM_IOEVENTFD 0
33 #define IVSHMEM_PEER 0
34 #define IVSHMEM_MASTER 1
36 #define IVSHMEM_REG_BAR_SIZE 0x100
38 //#define DEBUG_IVSHMEM
40 #define IVSHMEM_DPRINTF(fmt, ...) \
41 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
43 #define IVSHMEM_DPRINTF(fmt, ...)
51 typedef struct EventfdEntry {
56 typedef struct IVShmemState {
62 CharDriverState **eventfd_chr;
63 CharDriverState *server_chr;
64 MemoryRegion ivshmem_mmio;
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.
73 MemoryRegion msix_bar;
74 uint64_t ivshmem_size; /* size of shared memory region */
75 int shm_fd; /* shared memory file descriptor */
78 int nb_peers; /* how many guests we have space for */
79 int max_peer; /* maximum numbered peer */
84 EventfdEntry *eventfd_table;
86 Error *migration_blocker;
91 int role_val; /* scalar to avoid multiple string comparisons */
94 /* registers for the Inter-VM shared memory device */
95 enum ivshmem_registers {
102 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
103 unsigned int feature) {
104 return (ivs->features & (1 << feature));
107 static inline bool is_power_of_two(uint64_t x) {
108 return (x & (x - 1)) == 0;
111 /* accessing registers - based on rtl8139 */
112 static void ivshmem_update_irq(IVShmemState *s, int val)
115 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
117 /* don't print ISR resets */
119 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
120 isr ? 1 : 0, s->intrstatus, s->intrmask);
123 qemu_set_irq(s->dev.irq[0], (isr != 0));
126 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
128 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
132 ivshmem_update_irq(s, val);
135 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
137 uint32_t ret = s->intrmask;
139 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
144 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
146 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
150 ivshmem_update_irq(s, val);
154 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
156 uint32_t ret = s->intrstatus;
158 /* reading ISR clears all interrupts */
161 ivshmem_update_irq(s, 0);
166 static void ivshmem_io_write(void *opaque, target_phys_addr_t addr,
167 uint64_t val, unsigned size)
169 IVShmemState *s = opaque;
171 uint64_t write_one = 1;
172 uint16_t dest = val >> 16;
173 uint16_t vector = val & 0xff;
177 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
181 ivshmem_IntrMask_write(s, val);
185 ivshmem_IntrStatus_write(s, val);
189 /* check that dest VM ID is reasonable */
190 if (dest > s->max_peer) {
191 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
195 /* check doorbell range */
196 if (vector < s->peers[dest].nb_eventfds) {
197 IVSHMEM_DPRINTF("Writing %" PRId64 " to VM %d on vector %d\n",
198 write_one, dest, vector);
199 if (write(s->peers[dest].eventfds[vector],
200 &(write_one), 8) != 8) {
201 IVSHMEM_DPRINTF("error writing to eventfd\n");
206 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
210 static uint64_t ivshmem_io_read(void *opaque, target_phys_addr_t addr,
214 IVShmemState *s = opaque;
220 ret = ivshmem_IntrMask_read(s);
224 ret = ivshmem_IntrStatus_read(s);
228 /* return my VM ID if the memory is mapped */
237 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
244 static const MemoryRegionOps ivshmem_mmio_ops = {
245 .read = ivshmem_io_read,
246 .write = ivshmem_io_write,
247 .endianness = DEVICE_NATIVE_ENDIAN,
249 .min_access_size = 4,
250 .max_access_size = 4,
254 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
256 IVShmemState *s = opaque;
258 ivshmem_IntrStatus_write(s, *buf);
260 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
263 static int ivshmem_can_receive(void * opaque)
268 static void ivshmem_event(void *opaque, int event)
270 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
273 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
275 EventfdEntry *entry = opaque;
276 PCIDevice *pdev = entry->pdev;
278 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
279 msix_notify(pdev, entry->vector);
282 static CharDriverState* create_eventfd_chr_device(void * opaque, int eventfd,
285 /* create a event character device based on the passed eventfd */
286 IVShmemState *s = opaque;
287 CharDriverState * chr;
289 chr = qemu_chr_open_eventfd(eventfd);
292 fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
296 /* if MSI is supported we need multiple interrupts */
297 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
298 s->eventfd_table[vector].pdev = &s->dev;
299 s->eventfd_table[vector].vector = vector;
301 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
302 ivshmem_event, &s->eventfd_table[vector]);
304 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
312 static int check_shm_size(IVShmemState *s, int fd) {
313 /* check that the guest isn't going to try and map more memory than the
314 * the object has allocated return -1 to indicate error */
320 if (s->ivshmem_size > buf.st_size) {
322 "IVSHMEM ERROR: Requested memory size greater"
323 " than shared object size (%" PRIu64 " > %" PRIu64")\n",
324 s->ivshmem_size, (uint64_t)buf.st_size);
331 /* create the shared memory BAR when we are not using the server, so we can
332 * create the BAR and map the memory immediately */
333 static void create_shared_memory_BAR(IVShmemState *s, int fd) {
339 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
341 memory_region_init_ram_ptr(&s->ivshmem, "ivshmem.bar2",
342 s->ivshmem_size, ptr);
343 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
344 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
346 /* region for shared memory */
347 pci_register_bar(&s->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
350 static void close_guest_eventfds(IVShmemState *s, int posn)
352 int i, guest_curr_max;
354 guest_curr_max = s->peers[posn].nb_eventfds;
356 for (i = 0; i < guest_curr_max; i++) {
357 kvm_set_ioeventfd_mmio_long(s->peers[posn].eventfds[i],
358 s->mmio_addr + DOORBELL, (posn << 16) | i, 0);
359 close(s->peers[posn].eventfds[i]);
362 g_free(s->peers[posn].eventfds);
363 s->peers[posn].nb_eventfds = 0;
366 static void setup_ioeventfds(IVShmemState *s) {
370 for (i = 0; i <= s->max_peer; i++) {
371 for (j = 0; j < s->peers[i].nb_eventfds; j++) {
372 memory_region_add_eventfd(&s->ivshmem_mmio,
377 s->peers[i].eventfds[j]);
382 /* this function increase the dynamic storage need to store data about other
384 static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
388 old_nb_alloc = s->nb_peers;
390 while (new_min_size >= s->nb_peers)
391 s->nb_peers = s->nb_peers * 2;
393 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
394 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
396 /* zero out new pointers */
397 for (j = old_nb_alloc; j < s->nb_peers; j++) {
398 s->peers[j].eventfds = NULL;
399 s->peers[j].nb_eventfds = 0;
403 static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
405 IVShmemState *s = opaque;
406 int incoming_fd, tmp_fd;
407 int guest_max_eventfd;
410 memcpy(&incoming_posn, buf, sizeof(long));
411 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
412 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
413 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
415 /* make sure we have enough space for this guest */
416 if (incoming_posn >= s->nb_peers) {
417 increase_dynamic_storage(s, incoming_posn);
421 /* if posn is positive and unseen before then this is our posn*/
422 if ((incoming_posn >= 0) &&
423 (s->peers[incoming_posn].eventfds == NULL)) {
424 /* receive our posn */
425 s->vm_id = incoming_posn;
428 /* otherwise an fd == -1 means an existing guest has gone away */
429 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
430 close_guest_eventfds(s, incoming_posn);
435 /* because of the implementation of get_msgfd, we need a dup */
436 incoming_fd = dup(tmp_fd);
438 if (incoming_fd == -1) {
439 fprintf(stderr, "could not allocate file descriptor %s\n",
444 /* if the position is -1, then it's shared memory region fd */
445 if (incoming_posn == -1) {
451 if (check_shm_size(s, incoming_fd) == -1) {
455 /* mmap the region and map into the BAR2 */
456 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
458 memory_region_init_ram_ptr(&s->ivshmem,
459 "ivshmem.bar2", s->ivshmem_size, map_ptr);
460 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
462 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
463 s->ivshmem_offset, s->ivshmem_size);
465 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
467 /* only store the fd if it is successfully mapped */
468 s->shm_fd = incoming_fd;
473 /* each guest has an array of eventfds, and we keep track of how many
474 * guests for each VM */
475 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
477 if (guest_max_eventfd == 0) {
478 /* one eventfd per MSI vector */
479 s->peers[incoming_posn].eventfds = (int *) g_malloc(s->vectors *
483 /* this is an eventfd for a particular guest VM */
484 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
485 guest_max_eventfd, incoming_fd);
486 s->peers[incoming_posn].eventfds[guest_max_eventfd] = incoming_fd;
488 /* increment count for particular guest */
489 s->peers[incoming_posn].nb_eventfds++;
491 /* keep track of the maximum VM ID */
492 if (incoming_posn > s->max_peer) {
493 s->max_peer = incoming_posn;
496 if (incoming_posn == s->vm_id) {
497 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
498 s->peers[s->vm_id].eventfds[guest_max_eventfd],
502 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
503 if (kvm_set_ioeventfd_mmio_long(incoming_fd, s->mmio_addr + DOORBELL,
504 (incoming_posn << 16) | guest_max_eventfd, 1) < 0) {
505 fprintf(stderr, "ivshmem: ioeventfd not available\n");
512 static void ivshmem_reset(DeviceState *d)
514 IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
520 static uint64_t ivshmem_get_size(IVShmemState * s) {
525 value = strtoull(s->sizearg, &ptr, 10);
527 case 0: case 'M': case 'm':
534 fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
538 /* BARs must be a power of 2 */
539 if (!is_power_of_two(value)) {
540 fprintf(stderr, "ivshmem: size must be power of 2\n");
547 static void ivshmem_setup_msi(IVShmemState * s) {
551 /* allocate the MSI-X vectors */
553 memory_region_init(&s->msix_bar, "ivshmem-msix", 4096);
554 if (!msix_init(&s->dev, s->vectors, &s->msix_bar, 1, 0)) {
555 pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
557 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
559 IVSHMEM_DPRINTF("msix initialization failed\n");
563 /* 'activate' the vectors */
564 for (i = 0; i < s->vectors; i++) {
565 msix_vector_use(&s->dev, i);
568 /* allocate Qemu char devices for receiving interrupts */
569 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
572 static void ivshmem_save(QEMUFile* f, void *opaque)
574 IVShmemState *proxy = opaque;
576 IVSHMEM_DPRINTF("ivshmem_save\n");
577 pci_device_save(&proxy->dev, f);
579 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
580 msix_save(&proxy->dev, f);
582 qemu_put_be32(f, proxy->intrstatus);
583 qemu_put_be32(f, proxy->intrmask);
588 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
590 IVSHMEM_DPRINTF("ivshmem_load\n");
592 IVShmemState *proxy = opaque;
595 if (version_id > 0) {
599 if (proxy->role_val == IVSHMEM_PEER) {
600 fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
604 ret = pci_device_load(&proxy->dev, f);
609 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
610 msix_load(&proxy->dev, f);
611 for (i = 0; i < proxy->vectors; i++) {
612 msix_vector_use(&proxy->dev, i);
615 proxy->intrstatus = qemu_get_be32(f);
616 proxy->intrmask = qemu_get_be32(f);
622 static int pci_ivshmem_init(PCIDevice *dev)
624 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
627 if (s->sizearg == NULL)
628 s->ivshmem_size = 4 << 20; /* 4 MB default */
630 s->ivshmem_size = ivshmem_get_size(s);
633 register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
636 /* IRQFD requires MSI */
637 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
638 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
639 fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
643 /* check that role is reasonable */
645 if (strncmp(s->role, "peer", 5) == 0) {
646 s->role_val = IVSHMEM_PEER;
647 } else if (strncmp(s->role, "master", 7) == 0) {
648 s->role_val = IVSHMEM_MASTER;
650 fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
654 s->role_val = IVSHMEM_MASTER; /* default */
657 if (s->role_val == IVSHMEM_PEER) {
658 error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION, "ivshmem", "peer mode");
659 migrate_add_blocker(s->migration_blocker);
662 pci_conf = s->dev.config;
663 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
665 pci_config_set_interrupt_pin(pci_conf, 1);
669 memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
670 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
672 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
676 /* region for registers*/
677 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
680 memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
682 if ((s->server_chr != NULL) &&
683 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
684 /* if we get a UNIX socket as the parameter we will talk
685 * to the ivshmem server to receive the memory region */
687 if (s->shmobj != NULL) {
688 fprintf(stderr, "WARNING: do not specify both 'chardev' "
689 "and 'shm' with ivshmem\n");
692 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
693 s->server_chr->filename);
695 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
696 ivshmem_setup_msi(s);
699 /* we allocate enough space for 16 guests and grow as needed */
703 /* allocate/initialize space for interrupt handling */
704 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
706 pci_register_bar(&s->dev, 2,
707 PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
709 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
711 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
714 /* just map the file immediately, we're not using a server */
717 if (s->shmobj == NULL) {
718 fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
721 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
723 /* try opening with O_EXCL and if it succeeds zero the memory
724 * by truncating to 0 */
725 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
726 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
727 /* truncate file to length PCI device's memory */
728 if (ftruncate(fd, s->ivshmem_size) != 0) {
729 fprintf(stderr, "ivshmem: could not truncate shared file\n");
732 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
733 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
734 fprintf(stderr, "ivshmem: could not open shared file\n");
739 if (check_shm_size(s, fd) == -1) {
743 create_shared_memory_BAR(s, fd);
750 static int pci_ivshmem_uninit(PCIDevice *dev)
752 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
754 if (s->migration_blocker) {
755 migrate_del_blocker(s->migration_blocker);
756 error_free(s->migration_blocker);
759 memory_region_destroy(&s->ivshmem_mmio);
760 memory_region_del_subregion(&s->bar, &s->ivshmem);
761 vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
762 memory_region_destroy(&s->ivshmem);
763 memory_region_destroy(&s->bar);
764 unregister_savevm(&dev->qdev, "ivshmem", s);
769 static PCIDeviceInfo ivshmem_info = {
770 .qdev.name = "ivshmem",
771 .qdev.size = sizeof(IVShmemState),
772 .qdev.reset = ivshmem_reset,
773 .init = pci_ivshmem_init,
774 .exit = pci_ivshmem_uninit,
775 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
777 .class_id = PCI_CLASS_MEMORY_RAM,
778 .qdev.props = (Property[]) {
779 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
780 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
781 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
782 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
783 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
784 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
785 DEFINE_PROP_STRING("role", IVShmemState, role),
786 DEFINE_PROP_END_OF_LIST(),
790 static void ivshmem_register_devices(void)
792 pci_qdev_register(&ivshmem_info);
795 device_init(ivshmem_register_devices)