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.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
22 #include "hw/i386/pc.h"
23 #include "hw/pci/pci.h"
24 #include "hw/pci/msi.h"
25 #include "hw/pci/msix.h"
26 #include "sysemu/kvm.h"
27 #include "migration/migration.h"
28 #include "qemu/error-report.h"
29 #include "qemu/event_notifier.h"
30 #include "qemu/fifo8.h"
31 #include "sysemu/char.h"
32 #include "sysemu/hostmem.h"
33 #include "qapi/visitor.h"
34 #include "exec/ram_addr.h"
36 #include "hw/misc/ivshmem.h"
40 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
41 #define PCI_DEVICE_ID_IVSHMEM 0x1110
43 #define IVSHMEM_MAX_PEERS G_MAXUINT16
44 #define IVSHMEM_IOEVENTFD 0
47 #define IVSHMEM_PEER 0
48 #define IVSHMEM_MASTER 1
50 #define IVSHMEM_REG_BAR_SIZE 0x100
52 //#define DEBUG_IVSHMEM
54 #define IVSHMEM_DPRINTF(fmt, ...) \
55 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
57 #define IVSHMEM_DPRINTF(fmt, ...)
60 #define TYPE_IVSHMEM "ivshmem"
61 #define IVSHMEM(obj) \
62 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
66 EventNotifier
*eventfds
;
69 typedef struct MSIVector
{
74 typedef struct IVShmemState
{
79 HostMemoryBackend
*hostmem
;
83 CharDriverState
**eventfd_chr
;
84 CharDriverState
*server_chr
;
86 MemoryRegion ivshmem_mmio
;
88 /* We might need to register the BAR before we actually have the memory.
89 * So prepare a container MemoryRegion for the BAR immediately and
90 * add a subregion when we have the memory.
94 uint64_t ivshmem_size
; /* size of shared memory region */
95 uint32_t ivshmem_64bit
;
98 int nb_peers
; /* how many peers we have space for */
103 MSIVector
*msi_vectors
;
105 Error
*migration_blocker
;
110 int role_val
; /* scalar to avoid multiple string comparisons */
113 /* registers for the Inter-VM shared memory device */
114 enum ivshmem_registers
{
121 static inline uint32_t ivshmem_has_feature(IVShmemState
*ivs
,
122 unsigned int feature
) {
123 return (ivs
->features
& (1 << feature
));
126 /* accessing registers - based on rtl8139 */
127 static void ivshmem_update_irq(IVShmemState
*s
)
129 PCIDevice
*d
= PCI_DEVICE(s
);
131 isr
= (s
->intrstatus
& s
->intrmask
) & 0xffffffff;
133 /* don't print ISR resets */
135 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
136 isr
? 1 : 0, s
->intrstatus
, s
->intrmask
);
139 pci_set_irq(d
, (isr
!= 0));
142 static void ivshmem_IntrMask_write(IVShmemState
*s
, uint32_t val
)
144 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val
);
148 ivshmem_update_irq(s
);
151 static uint32_t ivshmem_IntrMask_read(IVShmemState
*s
)
153 uint32_t ret
= s
->intrmask
;
155 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret
);
160 static void ivshmem_IntrStatus_write(IVShmemState
*s
, uint32_t val
)
162 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val
);
166 ivshmem_update_irq(s
);
169 static uint32_t ivshmem_IntrStatus_read(IVShmemState
*s
)
171 uint32_t ret
= s
->intrstatus
;
173 /* reading ISR clears all interrupts */
176 ivshmem_update_irq(s
);
181 static void ivshmem_io_write(void *opaque
, hwaddr addr
,
182 uint64_t val
, unsigned size
)
184 IVShmemState
*s
= opaque
;
186 uint16_t dest
= val
>> 16;
187 uint16_t vector
= val
& 0xff;
191 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx
"\n", addr
);
195 ivshmem_IntrMask_write(s
, val
);
199 ivshmem_IntrStatus_write(s
, val
);
203 /* check that dest VM ID is reasonable */
204 if (dest
>= s
->nb_peers
) {
205 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest
);
209 /* check doorbell range */
210 if (vector
< s
->peers
[dest
].nb_eventfds
) {
211 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest
, vector
);
212 event_notifier_set(&s
->peers
[dest
].eventfds
[vector
]);
214 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
219 IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx
"\n", addr
);
223 static uint64_t ivshmem_io_read(void *opaque
, hwaddr addr
,
227 IVShmemState
*s
= opaque
;
233 ret
= ivshmem_IntrMask_read(s
);
237 ret
= ivshmem_IntrStatus_read(s
);
241 /* return my VM ID if the memory is mapped */
242 if (memory_region_is_mapped(&s
->ivshmem
)) {
250 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx
"\n", addr
);
257 static const MemoryRegionOps ivshmem_mmio_ops
= {
258 .read
= ivshmem_io_read
,
259 .write
= ivshmem_io_write
,
260 .endianness
= DEVICE_NATIVE_ENDIAN
,
262 .min_access_size
= 4,
263 .max_access_size
= 4,
267 static int ivshmem_can_receive(void * opaque
)
269 return sizeof(int64_t);
272 static void ivshmem_event(void *opaque
, int event
)
274 IVSHMEM_DPRINTF("ivshmem_event %d\n", event
);
277 static void ivshmem_vector_notify(void *opaque
)
279 MSIVector
*entry
= opaque
;
280 PCIDevice
*pdev
= entry
->pdev
;
281 IVShmemState
*s
= IVSHMEM(pdev
);
282 int vector
= entry
- s
->msi_vectors
;
283 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
285 if (!event_notifier_test_and_clear(n
)) {
289 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev
, vector
);
290 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
291 msix_notify(pdev
, vector
);
293 ivshmem_IntrStatus_write(s
, 1);
297 static int ivshmem_vector_unmask(PCIDevice
*dev
, unsigned vector
,
300 IVShmemState
*s
= IVSHMEM(dev
);
301 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
302 MSIVector
*v
= &s
->msi_vectors
[vector
];
305 IVSHMEM_DPRINTF("vector unmask %p %d\n", dev
, vector
);
307 ret
= kvm_irqchip_update_msi_route(kvm_state
, v
->virq
, msg
, dev
);
312 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, v
->virq
);
315 static void ivshmem_vector_mask(PCIDevice
*dev
, unsigned vector
)
317 IVShmemState
*s
= IVSHMEM(dev
);
318 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
321 IVSHMEM_DPRINTF("vector mask %p %d\n", dev
, vector
);
323 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
,
324 s
->msi_vectors
[vector
].virq
);
326 error_report("remove_irqfd_notifier_gsi failed");
330 static void ivshmem_vector_poll(PCIDevice
*dev
,
331 unsigned int vector_start
,
332 unsigned int vector_end
)
334 IVShmemState
*s
= IVSHMEM(dev
);
337 IVSHMEM_DPRINTF("vector poll %p %d-%d\n", dev
, vector_start
, vector_end
);
339 vector_end
= MIN(vector_end
, s
->vectors
);
341 for (vector
= vector_start
; vector
< vector_end
; vector
++) {
342 EventNotifier
*notifier
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
344 if (!msix_is_masked(dev
, vector
)) {
348 if (event_notifier_test_and_clear(notifier
)) {
349 msix_set_pending(dev
, vector
);
354 static void watch_vector_notifier(IVShmemState
*s
, EventNotifier
*n
,
357 int eventfd
= event_notifier_get_fd(n
);
359 /* if MSI is supported we need multiple interrupts */
360 s
->msi_vectors
[vector
].pdev
= PCI_DEVICE(s
);
362 qemu_set_fd_handler(eventfd
, ivshmem_vector_notify
,
363 NULL
, &s
->msi_vectors
[vector
]);
366 static int check_shm_size(IVShmemState
*s
, int fd
, Error
**errp
)
368 /* check that the guest isn't going to try and map more memory than the
369 * the object has allocated return -1 to indicate error */
373 if (fstat(fd
, &buf
) < 0) {
374 error_setg(errp
, "exiting: fstat on fd %d failed: %s",
375 fd
, strerror(errno
));
379 if (s
->ivshmem_size
> buf
.st_size
) {
380 error_setg(errp
, "Requested memory size greater"
381 " than shared object size (%" PRIu64
" > %" PRIu64
")",
382 s
->ivshmem_size
, (uint64_t)buf
.st_size
);
389 /* create the shared memory BAR when we are not using the server, so we can
390 * create the BAR and map the memory immediately */
391 static int create_shared_memory_BAR(IVShmemState
*s
, int fd
, uint8_t attr
,
396 ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
397 if (ptr
== MAP_FAILED
) {
398 error_setg_errno(errp
, errno
, "Failed to mmap shared memory");
402 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
), "ivshmem.bar2",
403 s
->ivshmem_size
, ptr
);
404 qemu_set_ram_fd(memory_region_get_ram_addr(&s
->ivshmem
), fd
);
405 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
406 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
408 /* region for shared memory */
409 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
414 static void ivshmem_add_eventfd(IVShmemState
*s
, int posn
, int i
)
416 memory_region_add_eventfd(&s
->ivshmem_mmio
,
421 &s
->peers
[posn
].eventfds
[i
]);
424 static void ivshmem_del_eventfd(IVShmemState
*s
, int posn
, int i
)
426 memory_region_del_eventfd(&s
->ivshmem_mmio
,
431 &s
->peers
[posn
].eventfds
[i
]);
434 static void close_peer_eventfds(IVShmemState
*s
, int posn
)
438 if (!ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
441 if (posn
< 0 || posn
>= s
->nb_peers
) {
442 error_report("invalid peer %d", posn
);
446 n
= s
->peers
[posn
].nb_eventfds
;
448 memory_region_transaction_begin();
449 for (i
= 0; i
< n
; i
++) {
450 ivshmem_del_eventfd(s
, posn
, i
);
452 memory_region_transaction_commit();
453 for (i
= 0; i
< n
; i
++) {
454 event_notifier_cleanup(&s
->peers
[posn
].eventfds
[i
]);
457 g_free(s
->peers
[posn
].eventfds
);
458 s
->peers
[posn
].nb_eventfds
= 0;
461 /* this function increase the dynamic storage need to store data about other
463 static int resize_peers(IVShmemState
*s
, int new_min_size
)
468 /* limit number of max peers */
469 if (new_min_size
<= 0 || new_min_size
> IVSHMEM_MAX_PEERS
) {
472 if (new_min_size
<= s
->nb_peers
) {
476 old_size
= s
->nb_peers
;
477 s
->nb_peers
= new_min_size
;
479 IVSHMEM_DPRINTF("bumping storage to %d peers\n", s
->nb_peers
);
481 s
->peers
= g_realloc(s
->peers
, s
->nb_peers
* sizeof(Peer
));
483 for (j
= old_size
; j
< s
->nb_peers
; j
++) {
484 s
->peers
[j
].eventfds
= g_new0(EventNotifier
, s
->vectors
);
485 s
->peers
[j
].nb_eventfds
= 0;
491 static bool fifo_update_and_get(IVShmemState
*s
, const uint8_t *buf
, int size
,
492 void *data
, size_t len
)
497 assert(len
<= sizeof(int64_t)); /* limitation of the fifo */
498 if (fifo8_is_empty(&s
->incoming_fifo
) && size
== len
) {
499 memcpy(data
, buf
, size
);
503 IVSHMEM_DPRINTF("short read of %d bytes\n", size
);
505 num
= MIN(size
, sizeof(int64_t) - fifo8_num_used(&s
->incoming_fifo
));
506 fifo8_push_all(&s
->incoming_fifo
, buf
, num
);
508 if (fifo8_num_used(&s
->incoming_fifo
) < len
) {
515 p
= fifo8_pop_buf(&s
->incoming_fifo
, len
, &num
);
518 memcpy(data
, p
, len
);
521 fifo8_push_all(&s
->incoming_fifo
, buf
, size
);
527 static bool fifo_update_and_get_i64(IVShmemState
*s
,
528 const uint8_t *buf
, int size
, int64_t *i64
)
530 if (fifo_update_and_get(s
, buf
, size
, i64
, sizeof(*i64
))) {
531 *i64
= GINT64_FROM_LE(*i64
);
538 static int ivshmem_add_kvm_msi_virq(IVShmemState
*s
, int vector
)
540 PCIDevice
*pdev
= PCI_DEVICE(s
);
541 MSIMessage msg
= msix_get_message(pdev
, vector
);
544 IVSHMEM_DPRINTF("ivshmem_add_kvm_msi_virq vector:%d\n", vector
);
546 if (s
->msi_vectors
[vector
].pdev
!= NULL
) {
550 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
, pdev
);
552 error_report("ivshmem: kvm_irqchip_add_msi_route failed");
556 s
->msi_vectors
[vector
].virq
= ret
;
557 s
->msi_vectors
[vector
].pdev
= pdev
;
562 static void setup_interrupt(IVShmemState
*s
, int vector
)
564 EventNotifier
*n
= &s
->peers
[s
->vm_id
].eventfds
[vector
];
565 bool with_irqfd
= kvm_msi_via_irqfd_enabled() &&
566 ivshmem_has_feature(s
, IVSHMEM_MSI
);
567 PCIDevice
*pdev
= PCI_DEVICE(s
);
569 IVSHMEM_DPRINTF("setting up interrupt for vector: %d\n", vector
);
572 IVSHMEM_DPRINTF("with eventfd");
573 watch_vector_notifier(s
, n
, vector
);
574 } else if (msix_enabled(pdev
)) {
575 IVSHMEM_DPRINTF("with irqfd");
576 if (ivshmem_add_kvm_msi_virq(s
, vector
) < 0) {
580 if (!msix_is_masked(pdev
, vector
)) {
581 kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
,
582 s
->msi_vectors
[vector
].virq
);
585 /* it will be delayed until msix is enabled, in write_config */
586 IVSHMEM_DPRINTF("with irqfd, delayed until msix enabled");
590 static void ivshmem_read(void *opaque
, const uint8_t *buf
, int size
)
592 IVShmemState
*s
= opaque
;
595 int64_t incoming_posn
;
599 if (!fifo_update_and_get_i64(s
, buf
, size
, &incoming_posn
)) {
603 if (incoming_posn
< -1) {
604 IVSHMEM_DPRINTF("invalid incoming_posn %" PRId64
"\n", incoming_posn
);
608 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
609 incoming_fd
= qemu_chr_fe_get_msgfd(s
->server_chr
);
610 IVSHMEM_DPRINTF("posn is %" PRId64
", fd is %d\n",
611 incoming_posn
, incoming_fd
);
613 /* make sure we have enough space for this peer */
614 if (incoming_posn
>= s
->nb_peers
) {
615 if (resize_peers(s
, incoming_posn
+ 1) < 0) {
616 error_report("failed to resize peers array");
617 if (incoming_fd
!= -1) {
624 peer
= &s
->peers
[incoming_posn
];
626 if (incoming_fd
== -1) {
627 /* if posn is positive and unseen before then this is our posn*/
628 if (incoming_posn
>= 0 && s
->vm_id
== -1) {
629 /* receive our posn */
630 s
->vm_id
= incoming_posn
;
632 /* otherwise an fd == -1 means an existing peer has gone away */
633 IVSHMEM_DPRINTF("posn %" PRId64
" has gone away\n", incoming_posn
);
634 close_peer_eventfds(s
, incoming_posn
);
639 /* if the position is -1, then it's shared memory region fd */
640 if (incoming_posn
== -1) {
643 if (memory_region_is_mapped(&s
->ivshmem
)) {
644 error_report("shm already initialized");
649 if (check_shm_size(s
, incoming_fd
, &err
) == -1) {
650 error_report_err(err
);
655 /* mmap the region and map into the BAR2 */
656 map_ptr
= mmap(0, s
->ivshmem_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
658 if (map_ptr
== MAP_FAILED
) {
659 error_report("Failed to mmap shared memory %s", strerror(errno
));
663 memory_region_init_ram_ptr(&s
->ivshmem
, OBJECT(s
),
664 "ivshmem.bar2", s
->ivshmem_size
, map_ptr
);
665 qemu_set_ram_fd(memory_region_get_ram_addr(&s
->ivshmem
),
667 vmstate_register_ram(&s
->ivshmem
, DEVICE(s
));
669 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64
"\n",
670 map_ptr
, s
->ivshmem_size
);
672 memory_region_add_subregion(&s
->bar
, 0, &s
->ivshmem
);
677 /* each peer has an associated array of eventfds, and we keep
678 * track of how many eventfds received so far */
679 /* get a new eventfd: */
680 if (peer
->nb_eventfds
>= s
->vectors
) {
681 error_report("Too many eventfd received, device has %d vectors",
687 new_eventfd
= peer
->nb_eventfds
++;
689 /* this is an eventfd for a particular peer VM */
690 IVSHMEM_DPRINTF("eventfds[%" PRId64
"][%d] = %d\n", incoming_posn
,
691 new_eventfd
, incoming_fd
);
692 event_notifier_init_fd(&peer
->eventfds
[new_eventfd
], incoming_fd
);
693 fcntl_setfl(incoming_fd
, O_NONBLOCK
); /* msix/irqfd poll non block */
695 if (incoming_posn
== s
->vm_id
) {
696 setup_interrupt(s
, new_eventfd
);
699 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
)) {
700 ivshmem_add_eventfd(s
, incoming_posn
, new_eventfd
);
704 static void ivshmem_check_version(void *opaque
, const uint8_t * buf
, int size
)
706 IVShmemState
*s
= opaque
;
710 if (!fifo_update_and_get_i64(s
, buf
, size
, &version
)) {
714 tmp
= qemu_chr_fe_get_msgfd(s
->server_chr
);
715 if (tmp
!= -1 || version
!= IVSHMEM_PROTOCOL_VERSION
) {
716 fprintf(stderr
, "incompatible version, you are connecting to a ivshmem-"
717 "server using a different protocol please check your setup\n");
718 qemu_chr_delete(s
->server_chr
);
719 s
->server_chr
= NULL
;
723 IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
724 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
, ivshmem_read
,
728 /* Select the MSI-X vectors used by device.
729 * ivshmem maps events to vectors statically, so
730 * we just enable all vectors on init and after reset. */
731 static void ivshmem_use_msix(IVShmemState
* s
)
733 PCIDevice
*d
= PCI_DEVICE(s
);
736 IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__
, msix_present(d
));
737 if (!msix_present(d
)) {
741 for (i
= 0; i
< s
->vectors
; i
++) {
742 msix_vector_use(d
, i
);
746 static void ivshmem_reset(DeviceState
*d
)
748 IVShmemState
*s
= IVSHMEM(d
);
755 static int ivshmem_setup_interrupts(IVShmemState
*s
)
757 /* allocate QEMU callback data for receiving interrupts */
758 s
->msi_vectors
= g_malloc0(s
->vectors
* sizeof(MSIVector
));
760 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
761 if (msix_init_exclusive_bar(PCI_DEVICE(s
), s
->vectors
, 1)) {
765 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s
->vectors
);
772 static void ivshmem_enable_irqfd(IVShmemState
*s
)
774 PCIDevice
*pdev
= PCI_DEVICE(s
);
777 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
778 ivshmem_add_kvm_msi_virq(s
, i
);
781 if (msix_set_vector_notifiers(pdev
,
782 ivshmem_vector_unmask
,
784 ivshmem_vector_poll
)) {
785 error_report("ivshmem: msix_set_vector_notifiers failed");
789 static void ivshmem_remove_kvm_msi_virq(IVShmemState
*s
, int vector
)
791 IVSHMEM_DPRINTF("ivshmem_remove_kvm_msi_virq vector:%d\n", vector
);
793 if (s
->msi_vectors
[vector
].pdev
== NULL
) {
797 /* it was cleaned when masked in the frontend. */
798 kvm_irqchip_release_virq(kvm_state
, s
->msi_vectors
[vector
].virq
);
800 s
->msi_vectors
[vector
].pdev
= NULL
;
803 static void ivshmem_disable_irqfd(IVShmemState
*s
)
805 PCIDevice
*pdev
= PCI_DEVICE(s
);
808 for (i
= 0; i
< s
->peers
[s
->vm_id
].nb_eventfds
; i
++) {
809 ivshmem_remove_kvm_msi_virq(s
, i
);
812 msix_unset_vector_notifiers(pdev
);
815 static void ivshmem_write_config(PCIDevice
*pdev
, uint32_t address
,
816 uint32_t val
, int len
)
818 IVShmemState
*s
= IVSHMEM(pdev
);
819 int is_enabled
, was_enabled
= msix_enabled(pdev
);
821 pci_default_write_config(pdev
, address
, val
, len
);
822 is_enabled
= msix_enabled(pdev
);
824 if (kvm_msi_via_irqfd_enabled() && s
->vm_id
!= -1) {
825 if (!was_enabled
&& is_enabled
) {
826 ivshmem_enable_irqfd(s
);
827 } else if (was_enabled
&& !is_enabled
) {
828 ivshmem_disable_irqfd(s
);
833 static void pci_ivshmem_realize(PCIDevice
*dev
, Error
**errp
)
835 IVShmemState
*s
= IVSHMEM(dev
);
837 uint8_t attr
= PCI_BASE_ADDRESS_SPACE_MEMORY
|
838 PCI_BASE_ADDRESS_MEM_PREFETCH
;
840 if (!!s
->server_chr
+ !!s
->shmobj
+ !!s
->hostmem
!= 1) {
842 "You must specify either 'shm', 'chardev' or 'x-memdev'");
850 g_warning("size argument ignored with hostmem");
853 mr
= host_memory_backend_get_memory(s
->hostmem
, errp
);
854 s
->ivshmem_size
= memory_region_size(mr
);
855 } else if (s
->sizearg
== NULL
) {
856 s
->ivshmem_size
= 4 << 20; /* 4 MB default */
859 int64_t size
= qemu_strtosz(s
->sizearg
, &end
);
860 if (size
< 0 || *end
!= '\0' || !is_power_of_2(size
)) {
861 error_setg(errp
, "Invalid size %s", s
->sizearg
);
864 s
->ivshmem_size
= size
;
867 fifo8_create(&s
->incoming_fifo
, sizeof(int64_t));
869 /* IRQFD requires MSI */
870 if (ivshmem_has_feature(s
, IVSHMEM_IOEVENTFD
) &&
871 !ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
872 error_setg(errp
, "ioeventfd/irqfd requires MSI");
876 /* check that role is reasonable */
878 if (strncmp(s
->role
, "peer", 5) == 0) {
879 s
->role_val
= IVSHMEM_PEER
;
880 } else if (strncmp(s
->role
, "master", 7) == 0) {
881 s
->role_val
= IVSHMEM_MASTER
;
883 error_setg(errp
, "'role' must be 'peer' or 'master'");
887 s
->role_val
= IVSHMEM_MASTER
; /* default */
890 if (s
->role_val
== IVSHMEM_PEER
) {
891 error_setg(&s
->migration_blocker
,
892 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
893 migrate_add_blocker(s
->migration_blocker
);
896 pci_conf
= dev
->config
;
897 pci_conf
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
;
899 pci_config_set_interrupt_pin(pci_conf
, 1);
901 memory_region_init_io(&s
->ivshmem_mmio
, OBJECT(s
), &ivshmem_mmio_ops
, s
,
902 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE
);
904 /* region for registers*/
905 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
,
908 memory_region_init(&s
->bar
, OBJECT(s
), "ivshmem-bar2-container", s
->ivshmem_size
);
909 if (s
->ivshmem_64bit
) {
910 attr
|= PCI_BASE_ADDRESS_MEM_TYPE_64
;
913 if (s
->hostmem
!= NULL
) {
916 IVSHMEM_DPRINTF("using hostmem\n");
918 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(s
->hostmem
), errp
);
919 vmstate_register_ram(mr
, DEVICE(s
));
920 memory_region_add_subregion(&s
->bar
, 0, mr
);
921 pci_register_bar(PCI_DEVICE(s
), 2, attr
, &s
->bar
);
922 } else if (s
->server_chr
!= NULL
) {
923 /* FIXME do not rely on what chr drivers put into filename */
924 if (strncmp(s
->server_chr
->filename
, "unix:", 5)) {
925 error_setg(errp
, "chardev is not a unix client socket");
929 /* if we get a UNIX socket as the parameter we will talk
930 * to the ivshmem server to receive the memory region */
932 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
933 s
->server_chr
->filename
);
935 if (ivshmem_setup_interrupts(s
) < 0) {
936 error_setg(errp
, "failed to initialize interrupts");
940 /* we allocate enough space for 16 peers and grow as needed */
944 pci_register_bar(dev
, 2, attr
, &s
->bar
);
946 s
->eventfd_chr
= g_malloc0(s
->vectors
* sizeof(CharDriverState
*));
948 qemu_chr_add_handlers(s
->server_chr
, ivshmem_can_receive
,
949 ivshmem_check_version
, ivshmem_event
, s
);
951 /* just map the file immediately, we're not using a server */
954 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s
->shmobj
);
956 /* try opening with O_EXCL and if it succeeds zero the memory
957 * by truncating to 0 */
958 if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
|O_EXCL
,
959 S_IRWXU
|S_IRWXG
|S_IRWXO
)) > 0) {
960 /* truncate file to length PCI device's memory */
961 if (ftruncate(fd
, s
->ivshmem_size
) != 0) {
962 error_report("could not truncate shared file");
965 } else if ((fd
= shm_open(s
->shmobj
, O_CREAT
|O_RDWR
,
966 S_IRWXU
|S_IRWXG
|S_IRWXO
)) < 0) {
967 error_setg(errp
, "could not open shared file");
971 if (check_shm_size(s
, fd
, errp
) == -1) {
975 create_shared_memory_BAR(s
, fd
, attr
, errp
);
979 static void pci_ivshmem_exit(PCIDevice
*dev
)
981 IVShmemState
*s
= IVSHMEM(dev
);
984 fifo8_destroy(&s
->incoming_fifo
);
986 if (s
->migration_blocker
) {
987 migrate_del_blocker(s
->migration_blocker
);
988 error_free(s
->migration_blocker
);
991 if (memory_region_is_mapped(&s
->ivshmem
)) {
993 void *addr
= memory_region_get_ram_ptr(&s
->ivshmem
);
996 if (munmap(addr
, s
->ivshmem_size
) == -1) {
997 error_report("Failed to munmap shared memory %s",
1001 fd
= qemu_get_ram_fd(memory_region_get_ram_addr(&s
->ivshmem
));
1007 vmstate_unregister_ram(&s
->ivshmem
, DEVICE(dev
));
1008 memory_region_del_subregion(&s
->bar
, &s
->ivshmem
);
1011 if (s
->eventfd_chr
) {
1012 for (i
= 0; i
< s
->vectors
; i
++) {
1013 if (s
->eventfd_chr
[i
]) {
1014 qemu_chr_free(s
->eventfd_chr
[i
]);
1017 g_free(s
->eventfd_chr
);
1021 for (i
= 0; i
< s
->nb_peers
; i
++) {
1022 close_peer_eventfds(s
, i
);
1027 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1028 msix_uninit_exclusive_bar(dev
);
1031 g_free(s
->msi_vectors
);
1034 static bool test_msix(void *opaque
, int version_id
)
1036 IVShmemState
*s
= opaque
;
1038 return ivshmem_has_feature(s
, IVSHMEM_MSI
);
1041 static bool test_no_msix(void *opaque
, int version_id
)
1043 return !test_msix(opaque
, version_id
);
1046 static int ivshmem_pre_load(void *opaque
)
1048 IVShmemState
*s
= opaque
;
1050 if (s
->role_val
== IVSHMEM_PEER
) {
1051 error_report("'peer' devices are not migratable");
1058 static int ivshmem_post_load(void *opaque
, int version_id
)
1060 IVShmemState
*s
= opaque
;
1062 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1063 ivshmem_use_msix(s
);
1069 static int ivshmem_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
1071 IVShmemState
*s
= opaque
;
1072 PCIDevice
*pdev
= PCI_DEVICE(s
);
1075 IVSHMEM_DPRINTF("ivshmem_load_old\n");
1077 if (version_id
!= 0) {
1081 if (s
->role_val
== IVSHMEM_PEER
) {
1082 error_report("'peer' devices are not migratable");
1086 ret
= pci_device_load(pdev
, f
);
1091 if (ivshmem_has_feature(s
, IVSHMEM_MSI
)) {
1093 ivshmem_use_msix(s
);
1095 s
->intrstatus
= qemu_get_be32(f
);
1096 s
->intrmask
= qemu_get_be32(f
);
1102 static const VMStateDescription ivshmem_vmsd
= {
1105 .minimum_version_id
= 1,
1106 .pre_load
= ivshmem_pre_load
,
1107 .post_load
= ivshmem_post_load
,
1108 .fields
= (VMStateField
[]) {
1109 VMSTATE_PCI_DEVICE(parent_obj
, IVShmemState
),
1111 VMSTATE_MSIX_TEST(parent_obj
, IVShmemState
, test_msix
),
1112 VMSTATE_UINT32_TEST(intrstatus
, IVShmemState
, test_no_msix
),
1113 VMSTATE_UINT32_TEST(intrmask
, IVShmemState
, test_no_msix
),
1115 VMSTATE_END_OF_LIST()
1117 .load_state_old
= ivshmem_load_old
,
1118 .minimum_version_id_old
= 0
1121 static Property ivshmem_properties
[] = {
1122 DEFINE_PROP_CHR("chardev", IVShmemState
, server_chr
),
1123 DEFINE_PROP_STRING("size", IVShmemState
, sizearg
),
1124 DEFINE_PROP_UINT32("vectors", IVShmemState
, vectors
, 1),
1125 DEFINE_PROP_BIT("ioeventfd", IVShmemState
, features
, IVSHMEM_IOEVENTFD
, false),
1126 DEFINE_PROP_BIT("msi", IVShmemState
, features
, IVSHMEM_MSI
, true),
1127 DEFINE_PROP_STRING("shm", IVShmemState
, shmobj
),
1128 DEFINE_PROP_STRING("role", IVShmemState
, role
),
1129 DEFINE_PROP_UINT32("use64", IVShmemState
, ivshmem_64bit
, 1),
1130 DEFINE_PROP_END_OF_LIST(),
1133 static void ivshmem_class_init(ObjectClass
*klass
, void *data
)
1135 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1136 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1138 k
->realize
= pci_ivshmem_realize
;
1139 k
->exit
= pci_ivshmem_exit
;
1140 k
->config_write
= ivshmem_write_config
;
1141 k
->vendor_id
= PCI_VENDOR_ID_IVSHMEM
;
1142 k
->device_id
= PCI_DEVICE_ID_IVSHMEM
;
1143 k
->class_id
= PCI_CLASS_MEMORY_RAM
;
1144 dc
->reset
= ivshmem_reset
;
1145 dc
->props
= ivshmem_properties
;
1146 dc
->vmsd
= &ivshmem_vmsd
;
1147 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1148 dc
->desc
= "Inter-VM shared memory";
1151 static void ivshmem_check_memdev_is_busy(Object
*obj
, const char *name
,
1152 Object
*val
, Error
**errp
)
1156 mr
= host_memory_backend_get_memory(MEMORY_BACKEND(val
), errp
);
1157 if (memory_region_is_mapped(mr
)) {
1158 char *path
= object_get_canonical_path_component(val
);
1159 error_setg(errp
, "can't use already busy memdev: %s", path
);
1162 qdev_prop_allow_set_link_before_realize(obj
, name
, val
, errp
);
1166 static void ivshmem_init(Object
*obj
)
1168 IVShmemState
*s
= IVSHMEM(obj
);
1170 object_property_add_link(obj
, "x-memdev", TYPE_MEMORY_BACKEND
,
1171 (Object
**)&s
->hostmem
,
1172 ivshmem_check_memdev_is_busy
,
1173 OBJ_PROP_LINK_UNREF_ON_RELEASE
,
1177 static const TypeInfo ivshmem_info
= {
1178 .name
= TYPE_IVSHMEM
,
1179 .parent
= TYPE_PCI_DEVICE
,
1180 .instance_size
= sizeof(IVShmemState
),
1181 .instance_init
= ivshmem_init
,
1182 .class_init
= ivshmem_class_init
,
1185 static void ivshmem_register_types(void)
1187 type_register_static(&ivshmem_info
);
1190 type_init(ivshmem_register_types
)