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"
26 #include "event_notifier.h"
29 #include <sys/types.h>
31 #define IVSHMEM_IOEVENTFD 0
34 #define IVSHMEM_PEER 0
35 #define IVSHMEM_MASTER 1
37 #define IVSHMEM_REG_BAR_SIZE 0x100
39 //#define DEBUG_IVSHMEM
41 #define IVSHMEM_DPRINTF(fmt, ...) \
42 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
44 #define IVSHMEM_DPRINTF(fmt, ...)
49 EventNotifier *eventfds;
52 typedef struct EventfdEntry {
57 typedef struct IVShmemState {
63 CharDriverState **eventfd_chr;
64 CharDriverState *server_chr;
65 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 uint16_t dest = val >> 16;
172 uint16_t vector = val & 0xff;
176 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
180 ivshmem_IntrMask_write(s, val);
184 ivshmem_IntrStatus_write(s, val);
188 /* check that dest VM ID is reasonable */
189 if (dest > s->max_peer) {
190 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
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]);
201 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
205 static uint64_t ivshmem_io_read(void *opaque, target_phys_addr_t addr,
209 IVShmemState *s = opaque;
215 ret = ivshmem_IntrMask_read(s);
219 ret = ivshmem_IntrStatus_read(s);
223 /* return my VM ID if the memory is mapped */
232 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
239 static const MemoryRegionOps ivshmem_mmio_ops = {
240 .read = ivshmem_io_read,
241 .write = ivshmem_io_write,
242 .endianness = DEVICE_NATIVE_ENDIAN,
244 .min_access_size = 4,
245 .max_access_size = 4,
249 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
251 IVShmemState *s = opaque;
253 ivshmem_IntrStatus_write(s, *buf);
255 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
258 static int ivshmem_can_receive(void * opaque)
263 static void ivshmem_event(void *opaque, int event)
265 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
268 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
270 EventfdEntry *entry = opaque;
271 PCIDevice *pdev = entry->pdev;
273 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
274 msix_notify(pdev, entry->vector);
277 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
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);
285 chr = qemu_chr_open_eventfd(eventfd);
288 fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
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;
297 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
298 ivshmem_event, &s->eventfd_table[vector]);
300 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
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 */
316 if (s->ivshmem_size > buf.st_size) {
318 "IVSHMEM ERROR: Requested memory size greater"
319 " than shared object size (%" PRIu64 " > %" PRIu64")\n",
320 s->ivshmem_size, (uint64_t)buf.st_size);
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) {
335 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
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);
342 /* region for shared memory */
343 pci_register_bar(&s->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
346 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
348 memory_region_add_eventfd(&s->ivshmem_mmio,
353 &s->peers[posn].eventfds[i]);
356 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
358 memory_region_del_eventfd(&s->ivshmem_mmio,
363 &s->peers[posn].eventfds[i]);
366 static void close_guest_eventfds(IVShmemState *s, int posn)
368 int i, guest_curr_max;
370 guest_curr_max = s->peers[posn].nb_eventfds;
372 memory_region_transaction_begin();
373 for (i = 0; i < guest_curr_max; i++) {
374 ivshmem_del_eventfd(s, posn, i);
376 memory_region_transaction_commit();
377 for (i = 0; i < guest_curr_max; i++) {
378 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
381 g_free(s->peers[posn].eventfds);
382 s->peers[posn].nb_eventfds = 0;
385 static void setup_ioeventfds(IVShmemState *s) {
389 for (i = 0; i <= s->max_peer; i++) {
390 for (j = 0; j < s->peers[i].nb_eventfds; j++) {
391 ivshmem_add_eventfd(s, i, j);
396 /* this function increase the dynamic storage need to store data about other
398 static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
402 old_nb_alloc = s->nb_peers;
404 while (new_min_size >= s->nb_peers)
405 s->nb_peers = s->nb_peers * 2;
407 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
408 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
410 /* zero out new pointers */
411 for (j = old_nb_alloc; j < s->nb_peers; j++) {
412 s->peers[j].eventfds = NULL;
413 s->peers[j].nb_eventfds = 0;
417 static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
419 IVShmemState *s = opaque;
420 int incoming_fd, tmp_fd;
421 int guest_max_eventfd;
424 memcpy(&incoming_posn, buf, sizeof(long));
425 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
426 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
427 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
429 /* make sure we have enough space for this guest */
430 if (incoming_posn >= s->nb_peers) {
431 increase_dynamic_storage(s, incoming_posn);
435 /* if posn is positive and unseen before then this is our posn*/
436 if ((incoming_posn >= 0) &&
437 (s->peers[incoming_posn].eventfds == NULL)) {
438 /* receive our posn */
439 s->vm_id = incoming_posn;
442 /* otherwise an fd == -1 means an existing guest has gone away */
443 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
444 close_guest_eventfds(s, incoming_posn);
449 /* because of the implementation of get_msgfd, we need a dup */
450 incoming_fd = dup(tmp_fd);
452 if (incoming_fd == -1) {
453 fprintf(stderr, "could not allocate file descriptor %s\n",
458 /* if the position is -1, then it's shared memory region fd */
459 if (incoming_posn == -1) {
465 if (check_shm_size(s, incoming_fd) == -1) {
469 /* mmap the region and map into the BAR2 */
470 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
472 memory_region_init_ram_ptr(&s->ivshmem,
473 "ivshmem.bar2", s->ivshmem_size, map_ptr);
474 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
476 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
477 s->ivshmem_offset, s->ivshmem_size);
479 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
481 /* only store the fd if it is successfully mapped */
482 s->shm_fd = incoming_fd;
487 /* each guest has an array of eventfds, and we keep track of how many
488 * guests for each VM */
489 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
491 if (guest_max_eventfd == 0) {
492 /* one eventfd per MSI vector */
493 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
496 /* this is an eventfd for a particular guest VM */
497 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
498 guest_max_eventfd, incoming_fd);
499 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
502 /* increment count for particular guest */
503 s->peers[incoming_posn].nb_eventfds++;
505 /* keep track of the maximum VM ID */
506 if (incoming_posn > s->max_peer) {
507 s->max_peer = incoming_posn;
510 if (incoming_posn == s->vm_id) {
511 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
512 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
516 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
517 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
523 /* Select the MSI-X vectors used by device.
524 * ivshmem maps events to vectors statically, so
525 * we just enable all vectors on init and after reset. */
526 static void ivshmem_use_msix(IVShmemState * s)
530 if (!msix_present(&s->dev)) {
534 for (i = 0; i < s->vectors; i++) {
535 msix_vector_use(&s->dev, i);
539 static void ivshmem_reset(DeviceState *d)
541 IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
548 static uint64_t ivshmem_get_size(IVShmemState * s) {
553 value = strtoull(s->sizearg, &ptr, 10);
555 case 0: case 'M': case 'm':
562 fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
566 /* BARs must be a power of 2 */
567 if (!is_power_of_two(value)) {
568 fprintf(stderr, "ivshmem: size must be power of 2\n");
575 static void ivshmem_setup_msi(IVShmemState * s)
577 memory_region_init(&s->msix_bar, "ivshmem-msix", 4096);
578 if (!msix_init(&s->dev, s->vectors, &s->msix_bar, 1, 0)) {
579 pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
581 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
583 IVSHMEM_DPRINTF("msix initialization failed\n");
587 /* allocate QEMU char devices for receiving interrupts */
588 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
593 static void ivshmem_save(QEMUFile* f, void *opaque)
595 IVShmemState *proxy = opaque;
597 IVSHMEM_DPRINTF("ivshmem_save\n");
598 pci_device_save(&proxy->dev, f);
600 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
601 msix_save(&proxy->dev, f);
603 qemu_put_be32(f, proxy->intrstatus);
604 qemu_put_be32(f, proxy->intrmask);
609 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
611 IVSHMEM_DPRINTF("ivshmem_load\n");
613 IVShmemState *proxy = opaque;
616 if (version_id > 0) {
620 if (proxy->role_val == IVSHMEM_PEER) {
621 fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
625 ret = pci_device_load(&proxy->dev, f);
630 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
631 msix_load(&proxy->dev, f);
632 ivshmem_use_msix(proxy);
634 proxy->intrstatus = qemu_get_be32(f);
635 proxy->intrmask = qemu_get_be32(f);
641 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
642 uint32_t val, int len)
644 pci_default_write_config(pci_dev, address, val, len);
645 msix_write_config(pci_dev, address, val, len);
648 static int pci_ivshmem_init(PCIDevice *dev)
650 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
653 if (s->sizearg == NULL)
654 s->ivshmem_size = 4 << 20; /* 4 MB default */
656 s->ivshmem_size = ivshmem_get_size(s);
659 register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
662 /* IRQFD requires MSI */
663 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
664 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
665 fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
669 /* check that role is reasonable */
671 if (strncmp(s->role, "peer", 5) == 0) {
672 s->role_val = IVSHMEM_PEER;
673 } else if (strncmp(s->role, "master", 7) == 0) {
674 s->role_val = IVSHMEM_MASTER;
676 fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
680 s->role_val = IVSHMEM_MASTER; /* default */
683 if (s->role_val == IVSHMEM_PEER) {
684 error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION, "ivshmem", "peer mode");
685 migrate_add_blocker(s->migration_blocker);
688 pci_conf = s->dev.config;
689 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
691 pci_config_set_interrupt_pin(pci_conf, 1);
695 memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
696 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
698 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
702 /* region for registers*/
703 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
706 memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
708 if ((s->server_chr != NULL) &&
709 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
710 /* if we get a UNIX socket as the parameter we will talk
711 * to the ivshmem server to receive the memory region */
713 if (s->shmobj != NULL) {
714 fprintf(stderr, "WARNING: do not specify both 'chardev' "
715 "and 'shm' with ivshmem\n");
718 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
719 s->server_chr->filename);
721 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
722 ivshmem_setup_msi(s);
725 /* we allocate enough space for 16 guests and grow as needed */
729 /* allocate/initialize space for interrupt handling */
730 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
732 pci_register_bar(&s->dev, 2,
733 PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
735 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
737 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
740 /* just map the file immediately, we're not using a server */
743 if (s->shmobj == NULL) {
744 fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
747 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
749 /* try opening with O_EXCL and if it succeeds zero the memory
750 * by truncating to 0 */
751 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
752 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
753 /* truncate file to length PCI device's memory */
754 if (ftruncate(fd, s->ivshmem_size) != 0) {
755 fprintf(stderr, "ivshmem: could not truncate shared file\n");
758 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
759 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
760 fprintf(stderr, "ivshmem: could not open shared file\n");
765 if (check_shm_size(s, fd) == -1) {
769 create_shared_memory_BAR(s, fd);
773 s->dev.config_write = ivshmem_write_config;
778 static int pci_ivshmem_uninit(PCIDevice *dev)
780 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
782 if (s->migration_blocker) {
783 migrate_del_blocker(s->migration_blocker);
784 error_free(s->migration_blocker);
787 memory_region_destroy(&s->ivshmem_mmio);
788 memory_region_del_subregion(&s->bar, &s->ivshmem);
789 vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
790 memory_region_destroy(&s->ivshmem);
791 memory_region_destroy(&s->bar);
792 unregister_savevm(&dev->qdev, "ivshmem", s);
797 static Property ivshmem_properties[] = {
798 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
799 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
800 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
801 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
802 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
803 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
804 DEFINE_PROP_STRING("role", IVShmemState, role),
805 DEFINE_PROP_END_OF_LIST(),
808 static void ivshmem_class_init(ObjectClass *klass, void *data)
810 DeviceClass *dc = DEVICE_CLASS(klass);
811 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
813 k->init = pci_ivshmem_init;
814 k->exit = pci_ivshmem_uninit;
815 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
816 k->device_id = 0x1110;
817 k->class_id = PCI_CLASS_MEMORY_RAM;
818 dc->reset = ivshmem_reset;
819 dc->props = ivshmem_properties;
822 static TypeInfo ivshmem_info = {
824 .parent = TYPE_PCI_DEVICE,
825 .instance_size = sizeof(IVShmemState),
826 .class_init = ivshmem_class_init,
829 static void ivshmem_register_types(void)
831 type_register_static(&ivshmem_info);
834 type_init(ivshmem_register_types)