acpi: factor out common pm_update_sci() into acpi core
[qemu.git] / hw / misc / ivshmem.c
blob8d144baa1eaecc09f75d6916b1e7c2b7d7b577fc
1 /*
2 * Inter-VM Shared Memory PCI device.
4 * Author:
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)
11 * and rtl8139.c
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 "hw/hw.h"
20 #include "hw/i386/pc.h"
21 #include "hw/pci/pci.h"
22 #include "hw/pci/msix.h"
23 #include "sysemu/kvm.h"
24 #include "migration/migration.h"
25 #include "qapi/qmp/qerror.h"
26 #include "qemu/event_notifier.h"
27 #include "sysemu/char.h"
29 #include <sys/mman.h>
30 #include <sys/types.h>
32 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
33 #define PCI_DEVICE_ID_IVSHMEM 0x1110
35 #define IVSHMEM_IOEVENTFD 0
36 #define IVSHMEM_MSI 1
38 #define IVSHMEM_PEER 0
39 #define IVSHMEM_MASTER 1
41 #define IVSHMEM_REG_BAR_SIZE 0x100
43 //#define DEBUG_IVSHMEM
44 #ifdef DEBUG_IVSHMEM
45 #define IVSHMEM_DPRINTF(fmt, ...) \
46 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define IVSHMEM_DPRINTF(fmt, ...)
49 #endif
51 #define TYPE_IVSHMEM "ivshmem"
52 #define IVSHMEM(obj) \
53 OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
55 typedef struct Peer {
56 int nb_eventfds;
57 EventNotifier *eventfds;
58 } Peer;
60 typedef struct EventfdEntry {
61 PCIDevice *pdev;
62 int vector;
63 } EventfdEntry;
65 typedef struct IVShmemState {
66 /*< private >*/
67 PCIDevice parent_obj;
68 /*< public >*/
70 uint32_t intrmask;
71 uint32_t intrstatus;
72 uint32_t doorbell;
74 CharDriverState **eventfd_chr;
75 CharDriverState *server_chr;
76 MemoryRegion ivshmem_mmio;
78 /* We might need to register the BAR before we actually have the memory.
79 * So prepare a container MemoryRegion for the BAR immediately and
80 * add a subregion when we have the memory.
82 MemoryRegion bar;
83 MemoryRegion ivshmem;
84 uint64_t ivshmem_size; /* size of shared memory region */
85 uint32_t ivshmem_attr;
86 uint32_t ivshmem_64bit;
87 int shm_fd; /* shared memory file descriptor */
89 Peer *peers;
90 int nb_peers; /* how many guests we have space for */
91 int max_peer; /* maximum numbered peer */
93 int vm_id;
94 uint32_t vectors;
95 uint32_t features;
96 EventfdEntry *eventfd_table;
98 Error *migration_blocker;
100 char * shmobj;
101 char * sizearg;
102 char * role;
103 int role_val; /* scalar to avoid multiple string comparisons */
104 } IVShmemState;
106 /* registers for the Inter-VM shared memory device */
107 enum ivshmem_registers {
108 INTRMASK = 0,
109 INTRSTATUS = 4,
110 IVPOSITION = 8,
111 DOORBELL = 12,
114 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
115 unsigned int feature) {
116 return (ivs->features & (1 << feature));
119 static inline bool is_power_of_two(uint64_t x) {
120 return (x & (x - 1)) == 0;
123 /* accessing registers - based on rtl8139 */
124 static void ivshmem_update_irq(IVShmemState *s, int val)
126 PCIDevice *d = PCI_DEVICE(s);
127 int isr;
128 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
130 /* don't print ISR resets */
131 if (isr) {
132 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
133 isr ? 1 : 0, s->intrstatus, s->intrmask);
136 pci_set_irq(d, (isr != 0));
139 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
141 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
143 s->intrmask = val;
145 ivshmem_update_irq(s, val);
148 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
150 uint32_t ret = s->intrmask;
152 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
154 return ret;
157 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
159 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
161 s->intrstatus = val;
163 ivshmem_update_irq(s, val);
166 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
168 uint32_t ret = s->intrstatus;
170 /* reading ISR clears all interrupts */
171 s->intrstatus = 0;
173 ivshmem_update_irq(s, 0);
175 return ret;
178 static void ivshmem_io_write(void *opaque, hwaddr addr,
179 uint64_t val, unsigned size)
181 IVShmemState *s = opaque;
183 uint16_t dest = val >> 16;
184 uint16_t vector = val & 0xff;
186 addr &= 0xfc;
188 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
189 switch (addr)
191 case INTRMASK:
192 ivshmem_IntrMask_write(s, val);
193 break;
195 case INTRSTATUS:
196 ivshmem_IntrStatus_write(s, val);
197 break;
199 case DOORBELL:
200 /* check that dest VM ID is reasonable */
201 if (dest > s->max_peer) {
202 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
203 break;
206 /* check doorbell range */
207 if (vector < s->peers[dest].nb_eventfds) {
208 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
209 event_notifier_set(&s->peers[dest].eventfds[vector]);
211 break;
212 default:
213 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
217 static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
218 unsigned size)
221 IVShmemState *s = opaque;
222 uint32_t ret;
224 switch (addr)
226 case INTRMASK:
227 ret = ivshmem_IntrMask_read(s);
228 break;
230 case INTRSTATUS:
231 ret = ivshmem_IntrStatus_read(s);
232 break;
234 case IVPOSITION:
235 /* return my VM ID if the memory is mapped */
236 if (s->shm_fd > 0) {
237 ret = s->vm_id;
238 } else {
239 ret = -1;
241 break;
243 default:
244 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
245 ret = 0;
248 return ret;
251 static const MemoryRegionOps ivshmem_mmio_ops = {
252 .read = ivshmem_io_read,
253 .write = ivshmem_io_write,
254 .endianness = DEVICE_NATIVE_ENDIAN,
255 .impl = {
256 .min_access_size = 4,
257 .max_access_size = 4,
261 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
263 IVShmemState *s = opaque;
265 ivshmem_IntrStatus_write(s, *buf);
267 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
270 static int ivshmem_can_receive(void * opaque)
272 return 8;
275 static void ivshmem_event(void *opaque, int event)
277 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
280 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
282 EventfdEntry *entry = opaque;
283 PCIDevice *pdev = entry->pdev;
285 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
286 msix_notify(pdev, entry->vector);
289 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
290 int vector)
292 /* create a event character device based on the passed eventfd */
293 IVShmemState *s = opaque;
294 CharDriverState * chr;
295 int eventfd = event_notifier_get_fd(n);
297 chr = qemu_chr_open_eventfd(eventfd);
299 if (chr == NULL) {
300 fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
301 exit(-1);
303 qemu_chr_fe_claim_no_fail(chr);
305 /* if MSI is supported we need multiple interrupts */
306 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
307 s->eventfd_table[vector].pdev = PCI_DEVICE(s);
308 s->eventfd_table[vector].vector = vector;
310 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
311 ivshmem_event, &s->eventfd_table[vector]);
312 } else {
313 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
314 ivshmem_event, s);
317 return chr;
321 static int check_shm_size(IVShmemState *s, int fd) {
322 /* check that the guest isn't going to try and map more memory than the
323 * the object has allocated return -1 to indicate error */
325 struct stat buf;
327 fstat(fd, &buf);
329 if (s->ivshmem_size > buf.st_size) {
330 fprintf(stderr,
331 "IVSHMEM ERROR: Requested memory size greater"
332 " than shared object size (%" PRIu64 " > %" PRIu64")\n",
333 s->ivshmem_size, (uint64_t)buf.st_size);
334 return -1;
335 } else {
336 return 0;
340 /* create the shared memory BAR when we are not using the server, so we can
341 * create the BAR and map the memory immediately */
342 static void create_shared_memory_BAR(IVShmemState *s, int fd) {
344 void * ptr;
346 s->shm_fd = fd;
348 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
350 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2",
351 s->ivshmem_size, ptr);
352 vmstate_register_ram(&s->ivshmem, DEVICE(s));
353 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
355 /* region for shared memory */
356 pci_register_bar(PCI_DEVICE(s), 2, s->ivshmem_attr, &s->bar);
359 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
361 memory_region_add_eventfd(&s->ivshmem_mmio,
362 DOORBELL,
364 true,
365 (posn << 16) | i,
366 &s->peers[posn].eventfds[i]);
369 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
371 memory_region_del_eventfd(&s->ivshmem_mmio,
372 DOORBELL,
374 true,
375 (posn << 16) | i,
376 &s->peers[posn].eventfds[i]);
379 static void close_guest_eventfds(IVShmemState *s, int posn)
381 int i, guest_curr_max;
383 if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
384 return;
387 guest_curr_max = s->peers[posn].nb_eventfds;
389 memory_region_transaction_begin();
390 for (i = 0; i < guest_curr_max; i++) {
391 ivshmem_del_eventfd(s, posn, i);
393 memory_region_transaction_commit();
394 for (i = 0; i < guest_curr_max; i++) {
395 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
398 g_free(s->peers[posn].eventfds);
399 s->peers[posn].nb_eventfds = 0;
402 /* this function increase the dynamic storage need to store data about other
403 * guests */
404 static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
406 int j, old_nb_alloc;
408 old_nb_alloc = s->nb_peers;
410 while (new_min_size >= s->nb_peers)
411 s->nb_peers = s->nb_peers * 2;
413 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
414 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
416 /* zero out new pointers */
417 for (j = old_nb_alloc; j < s->nb_peers; j++) {
418 s->peers[j].eventfds = NULL;
419 s->peers[j].nb_eventfds = 0;
423 static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
425 IVShmemState *s = opaque;
426 int incoming_fd, tmp_fd;
427 int guest_max_eventfd;
428 long incoming_posn;
430 memcpy(&incoming_posn, buf, sizeof(long));
431 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
432 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
433 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
435 /* make sure we have enough space for this guest */
436 if (incoming_posn >= s->nb_peers) {
437 increase_dynamic_storage(s, incoming_posn);
440 if (tmp_fd == -1) {
441 /* if posn is positive and unseen before then this is our posn*/
442 if ((incoming_posn >= 0) &&
443 (s->peers[incoming_posn].eventfds == NULL)) {
444 /* receive our posn */
445 s->vm_id = incoming_posn;
446 return;
447 } else {
448 /* otherwise an fd == -1 means an existing guest has gone away */
449 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
450 close_guest_eventfds(s, incoming_posn);
451 return;
455 /* because of the implementation of get_msgfd, we need a dup */
456 incoming_fd = dup(tmp_fd);
458 if (incoming_fd == -1) {
459 fprintf(stderr, "could not allocate file descriptor %s\n",
460 strerror(errno));
461 return;
464 /* if the position is -1, then it's shared memory region fd */
465 if (incoming_posn == -1) {
467 void * map_ptr;
469 s->max_peer = 0;
471 if (check_shm_size(s, incoming_fd) == -1) {
472 exit(-1);
475 /* mmap the region and map into the BAR2 */
476 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
477 incoming_fd, 0);
478 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
479 "ivshmem.bar2", s->ivshmem_size, map_ptr);
480 vmstate_register_ram(&s->ivshmem, DEVICE(s));
482 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
483 s->ivshmem_offset, s->ivshmem_size);
485 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
487 /* only store the fd if it is successfully mapped */
488 s->shm_fd = incoming_fd;
490 return;
493 /* each guest has an array of eventfds, and we keep track of how many
494 * guests for each VM */
495 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
497 if (guest_max_eventfd == 0) {
498 /* one eventfd per MSI vector */
499 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
502 /* this is an eventfd for a particular guest VM */
503 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
504 guest_max_eventfd, incoming_fd);
505 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
506 incoming_fd);
508 /* increment count for particular guest */
509 s->peers[incoming_posn].nb_eventfds++;
511 /* keep track of the maximum VM ID */
512 if (incoming_posn > s->max_peer) {
513 s->max_peer = incoming_posn;
516 if (incoming_posn == s->vm_id) {
517 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
518 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
519 guest_max_eventfd);
522 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
523 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
527 /* Select the MSI-X vectors used by device.
528 * ivshmem maps events to vectors statically, so
529 * we just enable all vectors on init and after reset. */
530 static void ivshmem_use_msix(IVShmemState * s)
532 PCIDevice *d = PCI_DEVICE(s);
533 int i;
535 if (!msix_present(d)) {
536 return;
539 for (i = 0; i < s->vectors; i++) {
540 msix_vector_use(d, i);
544 static void ivshmem_reset(DeviceState *d)
546 IVShmemState *s = IVSHMEM(d);
548 s->intrstatus = 0;
549 ivshmem_use_msix(s);
552 static uint64_t ivshmem_get_size(IVShmemState * s) {
554 uint64_t value;
555 char *ptr;
557 value = strtoull(s->sizearg, &ptr, 10);
558 switch (*ptr) {
559 case 0: case 'M': case 'm':
560 value <<= 20;
561 break;
562 case 'G': case 'g':
563 value <<= 30;
564 break;
565 default:
566 fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
567 exit(1);
570 /* BARs must be a power of 2 */
571 if (!is_power_of_two(value)) {
572 fprintf(stderr, "ivshmem: size must be power of 2\n");
573 exit(1);
576 return value;
579 static void ivshmem_setup_msi(IVShmemState * s)
581 if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
582 IVSHMEM_DPRINTF("msix initialization failed\n");
583 exit(1);
586 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
588 /* allocate QEMU char devices for receiving interrupts */
589 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
591 ivshmem_use_msix(s);
594 static void ivshmem_save(QEMUFile* f, void *opaque)
596 IVShmemState *proxy = opaque;
597 PCIDevice *pci_dev = PCI_DEVICE(proxy);
599 IVSHMEM_DPRINTF("ivshmem_save\n");
600 pci_device_save(pci_dev, f);
602 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
603 msix_save(pci_dev, f);
604 } else {
605 qemu_put_be32(f, proxy->intrstatus);
606 qemu_put_be32(f, proxy->intrmask);
611 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
613 IVSHMEM_DPRINTF("ivshmem_load\n");
615 IVShmemState *proxy = opaque;
616 PCIDevice *pci_dev = PCI_DEVICE(proxy);
617 int ret;
619 if (version_id > 0) {
620 return -EINVAL;
623 if (proxy->role_val == IVSHMEM_PEER) {
624 fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
625 return -EINVAL;
628 ret = pci_device_load(pci_dev, f);
629 if (ret) {
630 return ret;
633 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
634 msix_load(pci_dev, f);
635 ivshmem_use_msix(proxy);
636 } else {
637 proxy->intrstatus = qemu_get_be32(f);
638 proxy->intrmask = qemu_get_be32(f);
641 return 0;
644 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
645 uint32_t val, int len)
647 pci_default_write_config(pci_dev, address, val, len);
648 msix_write_config(pci_dev, address, val, len);
651 static int pci_ivshmem_init(PCIDevice *dev)
653 IVShmemState *s = IVSHMEM(dev);
654 uint8_t *pci_conf;
656 if (s->sizearg == NULL)
657 s->ivshmem_size = 4 << 20; /* 4 MB default */
658 else {
659 s->ivshmem_size = ivshmem_get_size(s);
662 register_savevm(DEVICE(dev), "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
663 dev);
665 /* IRQFD requires MSI */
666 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
667 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
668 fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
669 exit(1);
672 /* check that role is reasonable */
673 if (s->role) {
674 if (strncmp(s->role, "peer", 5) == 0) {
675 s->role_val = IVSHMEM_PEER;
676 } else if (strncmp(s->role, "master", 7) == 0) {
677 s->role_val = IVSHMEM_MASTER;
678 } else {
679 fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
680 exit(1);
682 } else {
683 s->role_val = IVSHMEM_MASTER; /* default */
686 if (s->role_val == IVSHMEM_PEER) {
687 error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
688 "peer mode", "ivshmem");
689 migrate_add_blocker(s->migration_blocker);
692 pci_conf = dev->config;
693 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
695 pci_config_set_interrupt_pin(pci_conf, 1);
697 s->shm_fd = 0;
699 memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
700 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
702 /* region for registers*/
703 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
704 &s->ivshmem_mmio);
706 memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
707 s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
708 PCI_BASE_ADDRESS_MEM_PREFETCH;
709 if (s->ivshmem_64bit) {
710 s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
713 if ((s->server_chr != NULL) &&
714 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
715 /* if we get a UNIX socket as the parameter we will talk
716 * to the ivshmem server to receive the memory region */
718 if (s->shmobj != NULL) {
719 fprintf(stderr, "WARNING: do not specify both 'chardev' "
720 "and 'shm' with ivshmem\n");
723 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
724 s->server_chr->filename);
726 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
727 ivshmem_setup_msi(s);
730 /* we allocate enough space for 16 guests and grow as needed */
731 s->nb_peers = 16;
732 s->vm_id = -1;
734 /* allocate/initialize space for interrupt handling */
735 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
737 pci_register_bar(dev, 2, s->ivshmem_attr, &s->bar);
739 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
741 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
742 ivshmem_event, s);
743 } else {
744 /* just map the file immediately, we're not using a server */
745 int fd;
747 if (s->shmobj == NULL) {
748 fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
749 exit(1);
752 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
754 /* try opening with O_EXCL and if it succeeds zero the memory
755 * by truncating to 0 */
756 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
757 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
758 /* truncate file to length PCI device's memory */
759 if (ftruncate(fd, s->ivshmem_size) != 0) {
760 fprintf(stderr, "ivshmem: could not truncate shared file\n");
763 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
764 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
765 fprintf(stderr, "ivshmem: could not open shared file\n");
766 exit(-1);
770 if (check_shm_size(s, fd) == -1) {
771 exit(-1);
774 create_shared_memory_BAR(s, fd);
778 dev->config_write = ivshmem_write_config;
780 return 0;
783 static void pci_ivshmem_uninit(PCIDevice *dev)
785 IVShmemState *s = IVSHMEM(dev);
787 if (s->migration_blocker) {
788 migrate_del_blocker(s->migration_blocker);
789 error_free(s->migration_blocker);
792 memory_region_destroy(&s->ivshmem_mmio);
793 memory_region_del_subregion(&s->bar, &s->ivshmem);
794 vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
795 memory_region_destroy(&s->ivshmem);
796 memory_region_destroy(&s->bar);
797 unregister_savevm(DEVICE(dev), "ivshmem", s);
800 static Property ivshmem_properties[] = {
801 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
802 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
803 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
804 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
805 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
806 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
807 DEFINE_PROP_STRING("role", IVShmemState, role),
808 DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
809 DEFINE_PROP_END_OF_LIST(),
812 static void ivshmem_class_init(ObjectClass *klass, void *data)
814 DeviceClass *dc = DEVICE_CLASS(klass);
815 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
817 k->init = pci_ivshmem_init;
818 k->exit = pci_ivshmem_uninit;
819 k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
820 k->device_id = PCI_DEVICE_ID_IVSHMEM;
821 k->class_id = PCI_CLASS_MEMORY_RAM;
822 dc->reset = ivshmem_reset;
823 dc->props = ivshmem_properties;
824 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
827 static const TypeInfo ivshmem_info = {
828 .name = TYPE_IVSHMEM,
829 .parent = TYPE_PCI_DEVICE,
830 .instance_size = sizeof(IVShmemState),
831 .class_init = ivshmem_class_init,
834 static void ivshmem_register_types(void)
836 type_register_static(&ivshmem_info);
839 type_init(ivshmem_register_types)