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"
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 uint64_t ivshmem_size
; /* size of shared memory region */
74 int shm_fd
; /* shared memory file descriptor */
77 int nb_peers
; /* how many guests we have space for */
78 int max_peer
; /* maximum numbered peer */
83 EventfdEntry
*eventfd_table
;
85 Error
*migration_blocker
;
90 int role_val
; /* scalar to avoid multiple string comparisons */
93 /* registers for the Inter-VM shared memory device */
94 enum ivshmem_registers
{
101 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
102 unsigned int feature
) {
103 return (ivs
->features
& (1 << feature
));
106 static inline bool is_power_of_two(uint64_t x
) {
107 return (x
& (x
- 1)) == 0;
110 /* accessing registers - based on rtl8139 */
111 static void ivshmem_update_irq(IVShmemState
*s
, int val
)
114 isr
= (s
->intrstatus
& s
->intrmask
) & 0xffffffff;
116 /* don't print ISR resets */
118 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
119 isr
? 1 : 0, s
->intrstatus
, s
->intrmask
);
122 qemu_set_irq(s
->dev
.irq
[0], (isr
!= 0));
125 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
127 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
131 ivshmem_update_irq(s
, val
);
134 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
136 uint32_t ret
= s
->intrmask
;
138 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret
);
143 static void ivshmem_IntrStatus_write(IVShmemState
*s
, uint32_t val
)
145 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val
);
149 ivshmem_update_irq(s
, val
);
153 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
155 uint32_t ret
= s
->intrstatus
;
157 /* reading ISR clears all interrupts */
160 ivshmem_update_irq(s
, 0);
165 static void ivshmem_io_write(void *opaque
, target_phys_addr_t addr
,
166 uint64_t val
, unsigned size
)
168 IVShmemState
*s
= opaque
;
170 uint16_t dest
= val
>> 16;
171 uint16_t vector
= val
& 0xff;
175 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
179 ivshmem_IntrMask_write(s
, val
);
183 ivshmem_IntrStatus_write(s
, val
);
187 /* check that dest VM ID is reasonable */
188 if (dest
> s
->max_peer
) {
189 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
193 /* check doorbell range */
194 if (vector
< s
->peers
[dest
].nb_eventfds
) {
195 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
196 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
200 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest
);
204 static uint64_t ivshmem_io_read(void *opaque
, target_phys_addr_t addr
,
208 IVShmemState
*s
= opaque
;
214 ret
= ivshmem_IntrMask_read(s
);
218 ret
= ivshmem_IntrStatus_read(s
);
222 /* return my VM ID if the memory is mapped */
231 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
238 static const MemoryRegionOps ivshmem_mmio_ops
= {
239 .read
= ivshmem_io_read
,
240 .write
= ivshmem_io_write
,
241 .endianness
= DEVICE_NATIVE_ENDIAN
,
243 .min_access_size
= 4,
244 .max_access_size
= 4,
248 static void ivshmem_receive(void *opaque
, const uint8_t *buf
, int size
)
250 IVShmemState
*s
= opaque
;
252 ivshmem_IntrStatus_write(s
, *buf
);
254 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf
);
257 static int ivshmem_can_receive(void * opaque
)
262 static void ivshmem_event(void *opaque
, int event
)
264 IVSHMEM_DPRINTF("ivshmem_event %d\n", event
);
267 static void fake_irqfd(void *opaque
, const uint8_t *buf
, int size
) {
269 EventfdEntry
*entry
= opaque
;
270 PCIDevice
*pdev
= entry
->pdev
;
272 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, entry
->vector
);
273 msix_notify(pdev
, entry
->vector
);
276 static CharDriverState
* create_eventfd_chr_device(void * opaque
, EventNotifier
*n
,
279 /* create a event character device based on the passed eventfd */
280 IVShmemState
*s
= opaque
;
281 CharDriverState
* chr
;
282 int eventfd
= event_notifier_get_fd(n
);
284 chr
= qemu_chr_open_eventfd(eventfd
);
287 fprintf(stderr
, "creating eventfd for eventfd %d failed\n", eventfd
);
291 /* if MSI is supported we need multiple interrupts */
292 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
293 s
->eventfd_table
[vector
].pdev
= &s
->dev
;
294 s
->eventfd_table
[vector
].vector
= vector
;
296 qemu_chr_add_handlers(chr
, ivshmem_can_receive
, fake_irqfd
,
297 ivshmem_event
, &s
->eventfd_table
[vector
]);
299 qemu_chr_add_handlers(chr
, ivshmem_can_receive
, ivshmem_receive
,
307 static int check_shm_size(IVShmemState
*s
, int fd
) {
308 /* check that the guest isn't going to try and map more memory than the
309 * the object has allocated return -1 to indicate error */
315 if (s
->ivshmem_size
> buf
.st_size
) {
317 "IVSHMEM ERROR: Requested memory size greater"
318 " than shared object size (%" PRIu64
" > %" PRIu64
")\n",
319 s
->ivshmem_size
, (uint64_t)buf
.st_size
);
326 /* create the shared memory BAR when we are not using the server, so we can
327 * create the BAR and map the memory immediately */
328 static void create_shared_memory_BAR(IVShmemState
*s
, int fd
) {
334 ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
336 memory_region_init_ram_ptr(&s
->ivshmem
, "ivshmem.bar2",
337 s
->ivshmem_size
, ptr
);
338 vmstate_register_ram(&s
->ivshmem
, &s
->dev
.qdev
);
339 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
341 /* region for shared memory */
342 pci_register_bar(&s
->dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->bar
);
345 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
347 memory_region_add_eventfd(&s
->ivshmem_mmio
,
352 &s
->peers
[posn
].eventfds
[i
]);
355 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
357 memory_region_del_eventfd(&s
->ivshmem_mmio
,
362 &s
->peers
[posn
].eventfds
[i
]);
365 static void close_guest_eventfds(IVShmemState
*s
, int posn
)
367 int i
, guest_curr_max
;
369 guest_curr_max
= s
->peers
[posn
].nb_eventfds
;
371 memory_region_transaction_begin();
372 for (i
= 0; i
< guest_curr_max
; i
++) {
373 ivshmem_del_eventfd(s
, posn
, i
);
375 memory_region_transaction_commit();
376 for (i
= 0; i
< guest_curr_max
; i
++) {
377 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
380 g_free(s
->peers
[posn
].eventfds
);
381 s
->peers
[posn
].nb_eventfds
= 0;
384 static void setup_ioeventfds(IVShmemState
*s
) {
388 for (i
= 0; i
<= s
->max_peer
; i
++) {
389 for (j
= 0; j
< s
->peers
[i
].nb_eventfds
; j
++) {
390 ivshmem_add_eventfd(s
, i
, j
);
395 /* this function increase the dynamic storage need to store data about other
397 static void increase_dynamic_storage(IVShmemState
*s
, int new_min_size
) {
401 old_nb_alloc
= s
->nb_peers
;
403 while (new_min_size
>= s
->nb_peers
)
404 s
->nb_peers
= s
->nb_peers
* 2;
406 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s
->nb_peers
);
407 s
->peers
= g_realloc(s
->peers
, s
->nb_peers
* sizeof(Peer
));
409 /* zero out new pointers */
410 for (j
= old_nb_alloc
; j
< s
->nb_peers
; j
++) {
411 s
->peers
[j
].eventfds
= NULL
;
412 s
->peers
[j
].nb_eventfds
= 0;
416 static void ivshmem_read(void *opaque
, const uint8_t * buf
, int flags
)
418 IVShmemState
*s
= opaque
;
419 int incoming_fd
, tmp_fd
;
420 int guest_max_eventfd
;
423 memcpy(&incoming_posn
, buf
, sizeof(long));
424 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
425 tmp_fd
= qemu_chr_fe_get_msgfd(s
->server_chr
);
426 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn
, tmp_fd
);
428 /* make sure we have enough space for this guest */
429 if (incoming_posn
>= s
->nb_peers
) {
430 increase_dynamic_storage(s
, incoming_posn
);
434 /* if posn is positive and unseen before then this is our posn*/
435 if ((incoming_posn
>= 0) &&
436 (s
->peers
[incoming_posn
].eventfds
== NULL
)) {
437 /* receive our posn */
438 s
->vm_id
= incoming_posn
;
441 /* otherwise an fd == -1 means an existing guest has gone away */
442 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn
);
443 close_guest_eventfds(s
, incoming_posn
);
448 /* because of the implementation of get_msgfd, we need a dup */
449 incoming_fd
= dup(tmp_fd
);
451 if (incoming_fd
== -1) {
452 fprintf(stderr
, "could not allocate file descriptor %s\n",
457 /* if the position is -1, then it's shared memory region fd */
458 if (incoming_posn
== -1) {
464 if (check_shm_size(s
, incoming_fd
) == -1) {
468 /* mmap the region and map into the BAR2 */
469 map_ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
471 memory_region_init_ram_ptr(&s
->ivshmem
,
472 "ivshmem.bar2", s
->ivshmem_size
, map_ptr
);
473 vmstate_register_ram(&s
->ivshmem
, &s
->dev
.qdev
);
475 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64
", size = %" PRIu64
"\n",
476 s
->ivshmem_offset
, s
->ivshmem_size
);
478 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
480 /* only store the fd if it is successfully mapped */
481 s
->shm_fd
= incoming_fd
;
486 /* each guest has an array of eventfds, and we keep track of how many
487 * guests for each VM */
488 guest_max_eventfd
= s
->peers
[incoming_posn
].nb_eventfds
;
490 if (guest_max_eventfd
== 0) {
491 /* one eventfd per MSI vector */
492 s
->peers
[incoming_posn
].eventfds
= g_new(EventNotifier
, s
->vectors
);
495 /* this is an eventfd for a particular guest VM */
496 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn
,
497 guest_max_eventfd
, incoming_fd
);
498 event_notifier_init_fd(&s
->peers
[incoming_posn
].eventfds
[guest_max_eventfd
],
501 /* increment count for particular guest */
502 s
->peers
[incoming_posn
].nb_eventfds
++;
504 /* keep track of the maximum VM ID */
505 if (incoming_posn
> s
->max_peer
) {
506 s
->max_peer
= incoming_posn
;
509 if (incoming_posn
== s
->vm_id
) {
510 s
->eventfd_chr
[guest_max_eventfd
] = create_eventfd_chr_device(s
,
511 &s
->peers
[s
->vm_id
].eventfds
[guest_max_eventfd
],
515 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
516 ivshmem_add_eventfd(s
, incoming_posn
, guest_max_eventfd
);
522 /* Select the MSI-X vectors used by device.
523 * ivshmem maps events to vectors statically, so
524 * we just enable all vectors on init and after reset. */
525 static void ivshmem_use_msix(IVShmemState
* s
)
529 if (!msix_present(&s
->dev
)) {
533 for (i
= 0; i
< s
->vectors
; i
++) {
534 msix_vector_use(&s
->dev
, i
);
538 static void ivshmem_reset(DeviceState
*d
)
540 IVShmemState
*s
= DO_UPCAST(IVShmemState
, dev
.qdev
, d
);
547 static uint64_t ivshmem_get_size(IVShmemState
* s
) {
552 value
= strtoull(s
->sizearg
, &ptr
, 10);
554 case 0: case 'M': case 'm':
561 fprintf(stderr
, "qemu: invalid ram size: %s\n", s
->sizearg
);
565 /* BARs must be a power of 2 */
566 if (!is_power_of_two(value
)) {
567 fprintf(stderr
, "ivshmem: size must be power of 2\n");
574 static void ivshmem_setup_msi(IVShmemState
* s
)
576 if (msix_init_exclusive_bar(&s
->dev
, s
->vectors
, 1)) {
577 IVSHMEM_DPRINTF("msix initialization failed\n");
581 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
583 /* allocate QEMU char devices for receiving interrupts */
584 s
->eventfd_table
= g_malloc0(s
->vectors
* sizeof(EventfdEntry
));
589 static void ivshmem_save(QEMUFile
* f
, void *opaque
)
591 IVShmemState
*proxy
= opaque
;
593 IVSHMEM_DPRINTF("ivshmem_save\n");
594 pci_device_save(&proxy
->dev
, f
);
596 if (ivshmem_has_feature(proxy
, IVSHMEM_MSI
)) {
597 msix_save(&proxy
->dev
, f
);
599 qemu_put_be32(f
, proxy
->intrstatus
);
600 qemu_put_be32(f
, proxy
->intrmask
);
605 static int ivshmem_load(QEMUFile
* f
, void *opaque
, int version_id
)
607 IVSHMEM_DPRINTF("ivshmem_load\n");
609 IVShmemState
*proxy
= opaque
;
612 if (version_id
> 0) {
616 if (proxy
->role_val
== IVSHMEM_PEER
) {
617 fprintf(stderr
, "ivshmem: 'peer' devices are not migratable\n");
621 ret
= pci_device_load(&proxy
->dev
, f
);
626 if (ivshmem_has_feature(proxy
, IVSHMEM_MSI
)) {
627 msix_load(&proxy
->dev
, f
);
628 ivshmem_use_msix(proxy
);
630 proxy
->intrstatus
= qemu_get_be32(f
);
631 proxy
->intrmask
= qemu_get_be32(f
);
637 static void ivshmem_write_config(PCIDevice
*pci_dev
, uint32_t address
,
638 uint32_t val
, int len
)
640 pci_default_write_config(pci_dev
, address
, val
, len
);
641 msix_write_config(pci_dev
, address
, val
, len
);
644 static int pci_ivshmem_init(PCIDevice
*dev
)
646 IVShmemState
*s
= DO_UPCAST(IVShmemState
, dev
, dev
);
649 if (s
->sizearg
== NULL
)
650 s
->ivshmem_size
= 4 << 20; /* 4 MB default */
652 s
->ivshmem_size
= ivshmem_get_size(s
);
655 register_savevm(&s
->dev
.qdev
, "ivshmem", 0, 0, ivshmem_save
, ivshmem_load
,
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");
665 /* check that role is reasonable */
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
;
672 fprintf(stderr
, "ivshmem: 'role' must be 'peer' or 'master'\n");
676 s
->role_val
= IVSHMEM_MASTER
; /* default */
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
);
684 pci_conf
= s
->dev
.config
;
685 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
687 pci_config_set_interrupt_pin(pci_conf
, 1);
691 memory_region_init_io(&s
->ivshmem_mmio
, &ivshmem_mmio_ops
, s
,
692 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
694 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
698 /* region for registers*/
699 pci_register_bar(&s
->dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
702 memory_region_init(&s
->bar
, "ivshmem-bar2-container", s
->ivshmem_size
);
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 */
709 if (s
->shmobj
!= NULL
) {
710 fprintf(stderr
, "WARNING: do not specify both 'chardev' "
711 "and 'shm' with ivshmem\n");
714 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
715 s
->server_chr
->filename
);
717 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
718 ivshmem_setup_msi(s
);
721 /* we allocate enough space for 16 guests and grow as needed */
725 /* allocate/initialize space for interrupt handling */
726 s
->peers
= g_malloc0(s
->nb_peers
* sizeof(Peer
));
728 pci_register_bar(&s
->dev
, 2,
729 PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->bar
);
731 s
->eventfd_chr
= g_malloc0(s
->vectors
* sizeof(CharDriverState
*));
733 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
, ivshmem_read
,
736 /* just map the file immediately, we're not using a server */
739 if (s
->shmobj
== NULL
) {
740 fprintf(stderr
, "Must specify 'chardev' or 'shm' to ivshmem\n");
743 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s
->shmobj
);
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");
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");
761 if (check_shm_size(s
, fd
) == -1) {
765 create_shared_memory_BAR(s
, fd
);
769 s
->dev
.config_write
= ivshmem_write_config
;
774 static void pci_ivshmem_uninit(PCIDevice
*dev
)
776 IVShmemState
*s
= DO_UPCAST(IVShmemState
, dev
, dev
);
778 if (s
->migration_blocker
) {
779 migrate_del_blocker(s
->migration_blocker
);
780 error_free(s
->migration_blocker
);
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
);
791 static Property ivshmem_properties
[] = {
792 DEFINE_PROP_CHR("chardev", IVShmemState
, server_chr
),
793 DEFINE_PROP_STRING("size", IVShmemState
, sizearg
),
794 DEFINE_PROP_UINT32("vectors", IVShmemState
, vectors
, 1),
795 DEFINE_PROP_BIT("ioeventfd", IVShmemState
, features
, IVSHMEM_IOEVENTFD
, false),
796 DEFINE_PROP_BIT("msi", IVShmemState
, features
, IVSHMEM_MSI
, true),
797 DEFINE_PROP_STRING("shm", IVShmemState
, shmobj
),
798 DEFINE_PROP_STRING("role", IVShmemState
, role
),
799 DEFINE_PROP_END_OF_LIST(),
802 static void ivshmem_class_init(ObjectClass
*klass
, void *data
)
804 DeviceClass
*dc
= DEVICE_CLASS(klass
);
805 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
807 k
->init
= pci_ivshmem_init
;
808 k
->exit
= pci_ivshmem_uninit
;
809 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
810 k
->device_id
= 0x1110;
811 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
812 dc
->reset
= ivshmem_reset
;
813 dc
->props
= ivshmem_properties
;
816 static TypeInfo ivshmem_info
= {
818 .parent
= TYPE_PCI_DEVICE
,
819 .instance_size
= sizeof(IVShmemState
),
820 .class_init
= ivshmem_class_init
,
823 static void ivshmem_register_types(void)
825 type_register_static(&ivshmem_info
);
828 type_init(ivshmem_register_types
)