2 * Inter-VM Shared Memory PCI device.
5 * Cam Macdonell <cam@cs.ualberta.ca>
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"
26 #include "event_notifier.h"
27 #include "qemu-char.h"
30 #include <sys/types.h>
32 #define IVSHMEM_IOEVENTFD 0
35 #define IVSHMEM_PEER 0
36 #define IVSHMEM_MASTER 1
38 #define IVSHMEM_REG_BAR_SIZE 0x100
40 //#define DEBUG_IVSHMEM
42 #define IVSHMEM_DPRINTF(fmt, ...) \
43 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
45 #define IVSHMEM_DPRINTF(fmt, ...)
50 EventNotifier *eventfds;
53 typedef struct EventfdEntry {
58 typedef struct IVShmemState {
64 CharDriverState **eventfd_chr;
65 CharDriverState *server_chr;
66 MemoryRegion ivshmem_mmio;
68 /* We might need to register the BAR before we actually have the memory.
69 * So prepare a container MemoryRegion for the BAR immediately and
70 * add a subregion when we have the memory.
74 uint64_t ivshmem_size; /* size of shared memory region */
75 uint32_t ivshmem_attr;
76 uint32_t ivshmem_64bit;
77 int shm_fd; /* shared memory file descriptor */
80 int nb_peers; /* how many guests we have space for */
81 int max_peer; /* maximum numbered peer */
86 EventfdEntry *eventfd_table;
88 Error *migration_blocker;
93 int role_val; /* scalar to avoid multiple string comparisons */
96 /* registers for the Inter-VM shared memory device */
97 enum ivshmem_registers {
104 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
105 unsigned int feature) {
106 return (ivs->features & (1 << feature));
109 static inline bool is_power_of_two(uint64_t x) {
110 return (x & (x - 1)) == 0;
113 /* accessing registers - based on rtl8139 */
114 static void ivshmem_update_irq(IVShmemState *s, int val)
117 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
119 /* don't print ISR resets */
121 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
122 isr ? 1 : 0, s->intrstatus, s->intrmask);
125 qemu_set_irq(s->dev.irq[0], (isr != 0));
128 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
130 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
134 ivshmem_update_irq(s, val);
137 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
139 uint32_t ret = s->intrmask;
141 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
146 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
148 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
152 ivshmem_update_irq(s, val);
155 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
157 uint32_t ret = s->intrstatus;
159 /* reading ISR clears all interrupts */
162 ivshmem_update_irq(s, 0);
167 static void ivshmem_io_write(void *opaque, hwaddr addr,
168 uint64_t val, unsigned size)
170 IVShmemState *s = opaque;
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("Notifying VM %d on vector %d\n", dest, vector);
198 event_notifier_set(&s->peers[dest].eventfds[vector]);
202 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
206 static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
210 IVShmemState *s = opaque;
216 ret = ivshmem_IntrMask_read(s);
220 ret = ivshmem_IntrStatus_read(s);
224 /* return my VM ID if the memory is mapped */
233 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
240 static const MemoryRegionOps ivshmem_mmio_ops = {
241 .read = ivshmem_io_read,
242 .write = ivshmem_io_write,
243 .endianness = DEVICE_NATIVE_ENDIAN,
245 .min_access_size = 4,
246 .max_access_size = 4,
250 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
252 IVShmemState *s = opaque;
254 ivshmem_IntrStatus_write(s, *buf);
256 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
259 static int ivshmem_can_receive(void * opaque)
264 static void ivshmem_event(void *opaque, int event)
266 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
269 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
271 EventfdEntry *entry = opaque;
272 PCIDevice *pdev = entry->pdev;
274 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
275 msix_notify(pdev, entry->vector);
278 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
281 /* create a event character device based on the passed eventfd */
282 IVShmemState *s = opaque;
283 CharDriverState * chr;
284 int eventfd = event_notifier_get_fd(n);
286 chr = qemu_chr_open_eventfd(eventfd);
289 fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
293 /* if MSI is supported we need multiple interrupts */
294 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
295 s->eventfd_table[vector].pdev = &s->dev;
296 s->eventfd_table[vector].vector = vector;
298 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
299 ivshmem_event, &s->eventfd_table[vector]);
301 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
309 static int check_shm_size(IVShmemState *s, int fd) {
310 /* check that the guest isn't going to try and map more memory than the
311 * the object has allocated return -1 to indicate error */
317 if (s->ivshmem_size > buf.st_size) {
319 "IVSHMEM ERROR: Requested memory size greater"
320 " than shared object size (%" PRIu64 " > %" PRIu64")\n",
321 s->ivshmem_size, (uint64_t)buf.st_size);
328 /* create the shared memory BAR when we are not using the server, so we can
329 * create the BAR and map the memory immediately */
330 static void create_shared_memory_BAR(IVShmemState *s, int fd) {
336 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
338 memory_region_init_ram_ptr(&s->ivshmem, "ivshmem.bar2",
339 s->ivshmem_size, ptr);
340 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
341 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
343 /* region for shared memory */
344 pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
347 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
349 memory_region_add_eventfd(&s->ivshmem_mmio,
354 &s->peers[posn].eventfds[i]);
357 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
359 memory_region_del_eventfd(&s->ivshmem_mmio,
364 &s->peers[posn].eventfds[i]);
367 static void close_guest_eventfds(IVShmemState *s, int posn)
369 int i, guest_curr_max;
371 if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
375 guest_curr_max = s->peers[posn].nb_eventfds;
377 memory_region_transaction_begin();
378 for (i = 0; i < guest_curr_max; i++) {
379 ivshmem_del_eventfd(s, posn, i);
381 memory_region_transaction_commit();
382 for (i = 0; i < guest_curr_max; i++) {
383 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
386 g_free(s->peers[posn].eventfds);
387 s->peers[posn].nb_eventfds = 0;
390 /* this function increase the dynamic storage need to store data about other
392 static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
396 old_nb_alloc = s->nb_peers;
398 while (new_min_size >= s->nb_peers)
399 s->nb_peers = s->nb_peers * 2;
401 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
402 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
404 /* zero out new pointers */
405 for (j = old_nb_alloc; j < s->nb_peers; j++) {
406 s->peers[j].eventfds = NULL;
407 s->peers[j].nb_eventfds = 0;
411 static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
413 IVShmemState *s = opaque;
414 int incoming_fd, tmp_fd;
415 int guest_max_eventfd;
418 memcpy(&incoming_posn, buf, sizeof(long));
419 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
420 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
421 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
423 /* make sure we have enough space for this guest */
424 if (incoming_posn >= s->nb_peers) {
425 increase_dynamic_storage(s, incoming_posn);
429 /* if posn is positive and unseen before then this is our posn*/
430 if ((incoming_posn >= 0) &&
431 (s->peers[incoming_posn].eventfds == NULL)) {
432 /* receive our posn */
433 s->vm_id = incoming_posn;
436 /* otherwise an fd == -1 means an existing guest has gone away */
437 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
438 close_guest_eventfds(s, incoming_posn);
443 /* because of the implementation of get_msgfd, we need a dup */
444 incoming_fd = dup(tmp_fd);
446 if (incoming_fd == -1) {
447 fprintf(stderr, "could not allocate file descriptor %s\n",
452 /* if the position is -1, then it's shared memory region fd */
453 if (incoming_posn == -1) {
459 if (check_shm_size(s, incoming_fd) == -1) {
463 /* mmap the region and map into the BAR2 */
464 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
466 memory_region_init_ram_ptr(&s->ivshmem,
467 "ivshmem.bar2", s->ivshmem_size, map_ptr);
468 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
470 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
471 s->ivshmem_offset, s->ivshmem_size);
473 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
475 /* only store the fd if it is successfully mapped */
476 s->shm_fd = incoming_fd;
481 /* each guest has an array of eventfds, and we keep track of how many
482 * guests for each VM */
483 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
485 if (guest_max_eventfd == 0) {
486 /* one eventfd per MSI vector */
487 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
490 /* this is an eventfd for a particular guest VM */
491 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
492 guest_max_eventfd, incoming_fd);
493 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
496 /* increment count for particular guest */
497 s->peers[incoming_posn].nb_eventfds++;
499 /* keep track of the maximum VM ID */
500 if (incoming_posn > s->max_peer) {
501 s->max_peer = incoming_posn;
504 if (incoming_posn == s->vm_id) {
505 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
506 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
510 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
511 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
515 /* Select the MSI-X vectors used by device.
516 * ivshmem maps events to vectors statically, so
517 * we just enable all vectors on init and after reset. */
518 static void ivshmem_use_msix(IVShmemState * s)
522 if (!msix_present(&s->dev)) {
526 for (i = 0; i < s->vectors; i++) {
527 msix_vector_use(&s->dev, i);
531 static void ivshmem_reset(DeviceState *d)
533 IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
539 static uint64_t ivshmem_get_size(IVShmemState * s) {
544 value = strtoull(s->sizearg, &ptr, 10);
546 case 0: case 'M': case 'm':
553 fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
557 /* BARs must be a power of 2 */
558 if (!is_power_of_two(value)) {
559 fprintf(stderr, "ivshmem: size must be power of 2\n");
566 static void ivshmem_setup_msi(IVShmemState * s)
568 if (msix_init_exclusive_bar(&s->dev, s->vectors, 1)) {
569 IVSHMEM_DPRINTF("msix initialization failed\n");
573 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
575 /* allocate QEMU char devices for receiving interrupts */
576 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
581 static void ivshmem_save(QEMUFile* f, void *opaque)
583 IVShmemState *proxy = opaque;
585 IVSHMEM_DPRINTF("ivshmem_save\n");
586 pci_device_save(&proxy->dev, f);
588 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
589 msix_save(&proxy->dev, f);
591 qemu_put_be32(f, proxy->intrstatus);
592 qemu_put_be32(f, proxy->intrmask);
597 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
599 IVSHMEM_DPRINTF("ivshmem_load\n");
601 IVShmemState *proxy = opaque;
604 if (version_id > 0) {
608 if (proxy->role_val == IVSHMEM_PEER) {
609 fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
613 ret = pci_device_load(&proxy->dev, f);
618 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
619 msix_load(&proxy->dev, f);
620 ivshmem_use_msix(proxy);
622 proxy->intrstatus = qemu_get_be32(f);
623 proxy->intrmask = qemu_get_be32(f);
629 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
630 uint32_t val, int len)
632 pci_default_write_config(pci_dev, address, val, len);
633 msix_write_config(pci_dev, address, val, len);
636 static int pci_ivshmem_init(PCIDevice *dev)
638 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
641 if (s->sizearg == NULL)
642 s->ivshmem_size = 4 << 20; /* 4 MB default */
644 s->ivshmem_size = ivshmem_get_size(s);
647 register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
650 /* IRQFD requires MSI */
651 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
652 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
653 fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
657 /* check that role is reasonable */
659 if (strncmp(s->role, "peer", 5) == 0) {
660 s->role_val = IVSHMEM_PEER;
661 } else if (strncmp(s->role, "master", 7) == 0) {
662 s->role_val = IVSHMEM_MASTER;
664 fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
668 s->role_val = IVSHMEM_MASTER; /* default */
671 if (s->role_val == IVSHMEM_PEER) {
672 error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
673 "peer mode", "ivshmem");
674 migrate_add_blocker(s->migration_blocker);
677 pci_conf = s->dev.config;
678 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
680 pci_config_set_interrupt_pin(pci_conf, 1);
684 memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
685 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
687 /* region for registers*/
688 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
691 memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
692 s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
693 PCI_BASE_ADDRESS_MEM_PREFETCH;
694 if (s->ivshmem_64bit) {
695 s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
698 if ((s->server_chr != NULL) &&
699 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
700 /* if we get a UNIX socket as the parameter we will talk
701 * to the ivshmem server to receive the memory region */
703 if (s->shmobj != NULL) {
704 fprintf(stderr, "WARNING: do not specify both 'chardev' "
705 "and 'shm' with ivshmem\n");
708 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
709 s->server_chr->filename);
711 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
712 ivshmem_setup_msi(s);
715 /* we allocate enough space for 16 guests and grow as needed */
719 /* allocate/initialize space for interrupt handling */
720 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
722 pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
724 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
726 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
729 /* just map the file immediately, we're not using a server */
732 if (s->shmobj == NULL) {
733 fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
736 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
738 /* try opening with O_EXCL and if it succeeds zero the memory
739 * by truncating to 0 */
740 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
741 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
742 /* truncate file to length PCI device's memory */
743 if (ftruncate(fd, s->ivshmem_size) != 0) {
744 fprintf(stderr, "ivshmem: could not truncate shared file\n");
747 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
748 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
749 fprintf(stderr, "ivshmem: could not open shared file\n");
754 if (check_shm_size(s, fd) == -1) {
758 create_shared_memory_BAR(s, fd);
762 s->dev.config_write = ivshmem_write_config;
767 static void pci_ivshmem_uninit(PCIDevice *dev)
769 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
771 if (s->migration_blocker) {
772 migrate_del_blocker(s->migration_blocker);
773 error_free(s->migration_blocker);
776 memory_region_destroy(&s->ivshmem_mmio);
777 memory_region_del_subregion(&s->bar, &s->ivshmem);
778 vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
779 memory_region_destroy(&s->ivshmem);
780 memory_region_destroy(&s->bar);
781 unregister_savevm(&dev->qdev, "ivshmem", s);
784 static Property ivshmem_properties[] = {
785 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
786 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
787 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
788 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
789 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
790 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
791 DEFINE_PROP_STRING("role", IVShmemState, role),
792 DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
793 DEFINE_PROP_END_OF_LIST(),
796 static void ivshmem_class_init(ObjectClass *klass, void *data)
798 DeviceClass *dc = DEVICE_CLASS(klass);
799 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
801 k->init = pci_ivshmem_init;
802 k->exit = pci_ivshmem_uninit;
803 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
804 k->device_id = 0x1110;
805 k->class_id = PCI_CLASS_MEMORY_RAM;
806 dc->reset = ivshmem_reset;
807 dc->props = ivshmem_properties;
810 static TypeInfo ivshmem_info = {
812 .parent = TYPE_PCI_DEVICE,
813 .instance_size = sizeof(IVShmemState),
814 .class_init = ivshmem_class_init,
817 static void ivshmem_register_types(void)
819 type_register_static(&ivshmem_info);
822 type_init(ivshmem_register_types)