]> Git Repo - qemu.git/blob - hw/misc/ivshmem.c
ivshmem: rename MSI eventfd_table
[qemu.git] / hw / misc / ivshmem.c
1 /*
2  * Inter-VM Shared Memory PCI device.
3  *
4  * Author:
5  *      Cam Macdonell <[email protected]>
6  *
7  * Based On: cirrus_vga.c
8  *          Copyright (c) 2004 Fabrice Bellard
9  *          Copyright (c) 2004 Makoto Suzuki (suzu)
10  *
11  *      and rtl8139.c
12  *          Copyright (c) 2006 Igor Kovalenko
13  *
14  * This code is licensed under the GNU GPL v2.
15  *
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.
18  */
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 "qemu/error-report.h"
26 #include "qemu/event_notifier.h"
27 #include "qemu/fifo8.h"
28 #include "sysemu/char.h"
29 #include "sysemu/hostmem.h"
30 #include "qapi/visitor.h"
31
32 #include "hw/misc/ivshmem.h"
33
34 #include <sys/mman.h>
35 #include <sys/types.h>
36 #include <limits.h>
37
38 #define PCI_VENDOR_ID_IVSHMEM   PCI_VENDOR_ID_REDHAT_QUMRANET
39 #define PCI_DEVICE_ID_IVSHMEM   0x1110
40
41 #define IVSHMEM_MAX_PEERS G_MAXUINT16
42 #define IVSHMEM_IOEVENTFD   0
43 #define IVSHMEM_MSI     1
44
45 #define IVSHMEM_PEER    0
46 #define IVSHMEM_MASTER  1
47
48 #define IVSHMEM_REG_BAR_SIZE 0x100
49
50 //#define DEBUG_IVSHMEM
51 #ifdef DEBUG_IVSHMEM
52 #define IVSHMEM_DPRINTF(fmt, ...)        \
53     do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
54 #else
55 #define IVSHMEM_DPRINTF(fmt, ...)
56 #endif
57
58 #define TYPE_IVSHMEM "ivshmem"
59 #define IVSHMEM(obj) \
60     OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
61
62 #define IVSHMEM_MEMDEV_PROP "memdev"
63
64 typedef struct Peer {
65     int nb_eventfds;
66     EventNotifier *eventfds;
67 } Peer;
68
69 typedef struct MSIVector {
70     PCIDevice *pdev;
71 } MSIVector;
72
73 typedef struct IVShmemState {
74     /*< private >*/
75     PCIDevice parent_obj;
76     /*< public >*/
77
78     HostMemoryBackend *hostmem;
79     uint32_t intrmask;
80     uint32_t intrstatus;
81
82     CharDriverState **eventfd_chr;
83     CharDriverState *server_chr;
84     Fifo8 incoming_fifo;
85     MemoryRegion ivshmem_mmio;
86
87     /* We might need to register the BAR before we actually have the memory.
88      * So prepare a container MemoryRegion for the BAR immediately and
89      * add a subregion when we have the memory.
90      */
91     MemoryRegion bar;
92     MemoryRegion ivshmem;
93     uint64_t ivshmem_size; /* size of shared memory region */
94     uint32_t ivshmem_64bit;
95
96     Peer *peers;
97     int nb_peers; /* how many peers we have space for */
98
99     int vm_id;
100     uint32_t vectors;
101     uint32_t features;
102     MSIVector *msi_vectors;
103
104     Error *migration_blocker;
105
106     char * shmobj;
107     char * sizearg;
108     char * role;
109     int role_val;   /* scalar to avoid multiple string comparisons */
110 } IVShmemState;
111
112 /* registers for the Inter-VM shared memory device */
113 enum ivshmem_registers {
114     INTRMASK = 0,
115     INTRSTATUS = 4,
116     IVPOSITION = 8,
117     DOORBELL = 12,
118 };
119
120 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
121                                                     unsigned int feature) {
122     return (ivs->features & (1 << feature));
123 }
124
125 /* accessing registers - based on rtl8139 */
126 static void ivshmem_update_irq(IVShmemState *s)
127 {
128     PCIDevice *d = PCI_DEVICE(s);
129     int isr;
130     isr = (s->intrstatus & s->intrmask) & 0xffffffff;
131
132     /* don't print ISR resets */
133     if (isr) {
134         IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
135                         isr ? 1 : 0, s->intrstatus, s->intrmask);
136     }
137
138     pci_set_irq(d, (isr != 0));
139 }
140
141 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
142 {
143     IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
144
145     s->intrmask = val;
146
147     ivshmem_update_irq(s);
148 }
149
150 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
151 {
152     uint32_t ret = s->intrmask;
153
154     IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
155
156     return ret;
157 }
158
159 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
160 {
161     IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
162
163     s->intrstatus = val;
164
165     ivshmem_update_irq(s);
166 }
167
168 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
169 {
170     uint32_t ret = s->intrstatus;
171
172     /* reading ISR clears all interrupts */
173     s->intrstatus = 0;
174
175     ivshmem_update_irq(s);
176
177     return ret;
178 }
179
180 static void ivshmem_io_write(void *opaque, hwaddr addr,
181                              uint64_t val, unsigned size)
182 {
183     IVShmemState *s = opaque;
184
185     uint16_t dest = val >> 16;
186     uint16_t vector = val & 0xff;
187
188     addr &= 0xfc;
189
190     IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
191     switch (addr)
192     {
193         case INTRMASK:
194             ivshmem_IntrMask_write(s, val);
195             break;
196
197         case INTRSTATUS:
198             ivshmem_IntrStatus_write(s, val);
199             break;
200
201         case DOORBELL:
202             /* check that dest VM ID is reasonable */
203             if (dest >= s->nb_peers) {
204                 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
205                 break;
206             }
207
208             /* check doorbell range */
209             if (vector < s->peers[dest].nb_eventfds) {
210                 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
211                 event_notifier_set(&s->peers[dest].eventfds[vector]);
212             } else {
213                 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
214                                 vector, dest);
215             }
216             break;
217         default:
218             IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx "\n", addr);
219     }
220 }
221
222 static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
223                                 unsigned size)
224 {
225
226     IVShmemState *s = opaque;
227     uint32_t ret;
228
229     switch (addr)
230     {
231         case INTRMASK:
232             ret = ivshmem_IntrMask_read(s);
233             break;
234
235         case INTRSTATUS:
236             ret = ivshmem_IntrStatus_read(s);
237             break;
238
239         case IVPOSITION:
240             /* return my VM ID if the memory is mapped */
241             if (memory_region_is_mapped(&s->ivshmem)) {
242                 ret = s->vm_id;
243             } else {
244                 ret = -1;
245             }
246             break;
247
248         default:
249             IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
250             ret = 0;
251     }
252
253     return ret;
254 }
255
256 static const MemoryRegionOps ivshmem_mmio_ops = {
257     .read = ivshmem_io_read,
258     .write = ivshmem_io_write,
259     .endianness = DEVICE_NATIVE_ENDIAN,
260     .impl = {
261         .min_access_size = 4,
262         .max_access_size = 4,
263     },
264 };
265
266 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
267 {
268     IVShmemState *s = opaque;
269
270     IVSHMEM_DPRINTF("ivshmem_receive 0x%02x size: %d\n", *buf, size);
271
272     ivshmem_IntrStatus_write(s, *buf);
273 }
274
275 static int ivshmem_can_receive(void * opaque)
276 {
277     return sizeof(long);
278 }
279
280 static void ivshmem_event(void *opaque, int event)
281 {
282     IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
283 }
284
285 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
286
287     MSIVector *entry = opaque;
288     PCIDevice *pdev = entry->pdev;
289     IVShmemState *s = IVSHMEM(pdev);
290     int vector = entry - s->msi_vectors;
291
292     IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, vector);
293     msix_notify(pdev, vector);
294 }
295
296 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
297                                                   int vector)
298 {
299     /* create a event character device based on the passed eventfd */
300     IVShmemState *s = opaque;
301     CharDriverState * chr;
302     int eventfd = event_notifier_get_fd(n);
303
304     chr = qemu_chr_open_eventfd(eventfd);
305
306     if (chr == NULL) {
307         error_report("creating chardriver for eventfd %d failed", eventfd);
308         return NULL;
309     }
310     qemu_chr_fe_claim_no_fail(chr);
311
312     /* if MSI is supported we need multiple interrupts */
313     if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
314         s->msi_vectors[vector].pdev = PCI_DEVICE(s);
315
316         qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
317                       ivshmem_event, &s->msi_vectors[vector]);
318     } else {
319         qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
320                       ivshmem_event, s);
321     }
322
323     return chr;
324
325 }
326
327 static int check_shm_size(IVShmemState *s, int fd, Error **errp)
328 {
329     /* check that the guest isn't going to try and map more memory than the
330      * the object has allocated return -1 to indicate error */
331
332     struct stat buf;
333
334     if (fstat(fd, &buf) < 0) {
335         error_setg(errp, "exiting: fstat on fd %d failed: %s",
336                    fd, strerror(errno));
337         return -1;
338     }
339
340     if (s->ivshmem_size > buf.st_size) {
341         error_setg(errp, "Requested memory size greater"
342                    " than shared object size (%" PRIu64 " > %" PRIu64")",
343                    s->ivshmem_size, (uint64_t)buf.st_size);
344         return -1;
345     } else {
346         return 0;
347     }
348 }
349
350 /* create the shared memory BAR when we are not using the server, so we can
351  * create the BAR and map the memory immediately */
352 static int create_shared_memory_BAR(IVShmemState *s, int fd, uint8_t attr,
353                                     Error **errp)
354 {
355     void * ptr;
356
357     ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
358     if (ptr == MAP_FAILED) {
359         error_setg_errno(errp, errno, "Failed to mmap shared memory");
360         return -1;
361     }
362
363     memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2",
364                                s->ivshmem_size, ptr);
365     vmstate_register_ram(&s->ivshmem, DEVICE(s));
366     memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
367
368     /* region for shared memory */
369     pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
370
371     return 0;
372 }
373
374 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
375 {
376     memory_region_add_eventfd(&s->ivshmem_mmio,
377                               DOORBELL,
378                               4,
379                               true,
380                               (posn << 16) | i,
381                               &s->peers[posn].eventfds[i]);
382 }
383
384 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
385 {
386     memory_region_del_eventfd(&s->ivshmem_mmio,
387                               DOORBELL,
388                               4,
389                               true,
390                               (posn << 16) | i,
391                               &s->peers[posn].eventfds[i]);
392 }
393
394 static void close_peer_eventfds(IVShmemState *s, int posn)
395 {
396     int i, n;
397
398     if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
399         return;
400     }
401     if (posn < 0 || posn >= s->nb_peers) {
402         error_report("invalid peer %d", posn);
403         return;
404     }
405
406     n = s->peers[posn].nb_eventfds;
407
408     memory_region_transaction_begin();
409     for (i = 0; i < n; i++) {
410         ivshmem_del_eventfd(s, posn, i);
411     }
412     memory_region_transaction_commit();
413     for (i = 0; i < n; i++) {
414         event_notifier_cleanup(&s->peers[posn].eventfds[i]);
415     }
416
417     g_free(s->peers[posn].eventfds);
418     s->peers[posn].nb_eventfds = 0;
419 }
420
421 /* this function increase the dynamic storage need to store data about other
422  * peers */
423 static int resize_peers(IVShmemState *s, int new_min_size)
424 {
425
426     int j, old_size;
427
428     /* limit number of max peers */
429     if (new_min_size <= 0 || new_min_size > IVSHMEM_MAX_PEERS) {
430         return -1;
431     }
432     if (new_min_size <= s->nb_peers) {
433         return 0;
434     }
435
436     old_size = s->nb_peers;
437     s->nb_peers = new_min_size;
438
439     IVSHMEM_DPRINTF("bumping storage to %d peers\n", s->nb_peers);
440
441     s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
442
443     for (j = old_size; j < s->nb_peers; j++) {
444         s->peers[j].eventfds = g_new0(EventNotifier, s->vectors);
445         s->peers[j].nb_eventfds = 0;
446     }
447
448     return 0;
449 }
450
451 static bool fifo_update_and_get(IVShmemState *s, const uint8_t *buf, int size,
452                                 void *data, size_t len)
453 {
454     const uint8_t *p;
455     uint32_t num;
456
457     assert(len <= sizeof(long)); /* limitation of the fifo */
458     if (fifo8_is_empty(&s->incoming_fifo) && size == len) {
459         memcpy(data, buf, size);
460         return true;
461     }
462
463     IVSHMEM_DPRINTF("short read of %d bytes\n", size);
464
465     num = MIN(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo));
466     fifo8_push_all(&s->incoming_fifo, buf, num);
467
468     if (fifo8_num_used(&s->incoming_fifo) < len) {
469         assert(num == 0);
470         return false;
471     }
472
473     size -= num;
474     buf += num;
475     p = fifo8_pop_buf(&s->incoming_fifo, len, &num);
476     assert(num == len);
477
478     memcpy(data, p, len);
479
480     if (size > 0) {
481         fifo8_push_all(&s->incoming_fifo, buf, size);
482     }
483
484     return true;
485 }
486
487 static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
488 {
489     IVShmemState *s = opaque;
490     int incoming_fd;
491     int new_eventfd;
492     long incoming_posn;
493     Error *err = NULL;
494     Peer *peer;
495
496     if (!fifo_update_and_get(s, buf, size,
497                              &incoming_posn, sizeof(incoming_posn))) {
498         return;
499     }
500
501     if (incoming_posn < -1) {
502         IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn);
503         return;
504     }
505
506     /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
507     incoming_fd = qemu_chr_fe_get_msgfd(s->server_chr);
508     IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, incoming_fd);
509
510     /* make sure we have enough space for this peer */
511     if (incoming_posn >= s->nb_peers) {
512         if (resize_peers(s, incoming_posn + 1) < 0) {
513             error_report("failed to resize peers array");
514             if (incoming_fd != -1) {
515                 close(incoming_fd);
516             }
517             return;
518         }
519     }
520
521     peer = &s->peers[incoming_posn];
522
523     if (incoming_fd == -1) {
524         /* if posn is positive and unseen before then this is our posn*/
525         if (incoming_posn >= 0 && s->vm_id == -1) {
526             /* receive our posn */
527             s->vm_id = incoming_posn;
528         } else {
529             /* otherwise an fd == -1 means an existing peer has gone away */
530             IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
531             close_peer_eventfds(s, incoming_posn);
532         }
533         return;
534     }
535
536     /* if the position is -1, then it's shared memory region fd */
537     if (incoming_posn == -1) {
538         void * map_ptr;
539
540         if (memory_region_is_mapped(&s->ivshmem)) {
541             error_report("shm already initialized");
542             close(incoming_fd);
543             return;
544         }
545
546         if (check_shm_size(s, incoming_fd, &err) == -1) {
547             error_report_err(err);
548             close(incoming_fd);
549             return;
550         }
551
552         /* mmap the region and map into the BAR2 */
553         map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
554                                                             incoming_fd, 0);
555         if (map_ptr == MAP_FAILED) {
556             error_report("Failed to mmap shared memory %s", strerror(errno));
557             close(incoming_fd);
558             return;
559         }
560         memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
561                                    "ivshmem.bar2", s->ivshmem_size, map_ptr);
562         vmstate_register_ram(&s->ivshmem, DEVICE(s));
563
564         IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",
565                         map_ptr, s->ivshmem_size);
566
567         memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
568
569         close(incoming_fd);
570         return;
571     }
572
573     /* each peer has an associated array of eventfds, and we keep
574      * track of how many eventfds received so far */
575     /* get a new eventfd: */
576     if (peer->nb_eventfds >= s->vectors) {
577         error_report("Too many eventfd received, device has %d vectors",
578                      s->vectors);
579         close(incoming_fd);
580         return;
581     }
582
583     new_eventfd = peer->nb_eventfds++;
584
585     /* this is an eventfd for a particular peer VM */
586     IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
587                     new_eventfd, incoming_fd);
588     event_notifier_init_fd(&peer->eventfds[new_eventfd], incoming_fd);
589
590     if (incoming_posn == s->vm_id) {
591         s->eventfd_chr[new_eventfd] = create_eventfd_chr_device(s,
592                    &s->peers[s->vm_id].eventfds[new_eventfd],
593                    new_eventfd);
594     }
595
596     if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
597         ivshmem_add_eventfd(s, incoming_posn, new_eventfd);
598     }
599 }
600
601 static void ivshmem_check_version(void *opaque, const uint8_t * buf, int size)
602 {
603     IVShmemState *s = opaque;
604     int tmp;
605     long version;
606
607     if (!fifo_update_and_get(s, buf, size,
608                              &version, sizeof(version))) {
609         return;
610     }
611
612     tmp = qemu_chr_fe_get_msgfd(s->server_chr);
613     if (tmp != -1 || version != IVSHMEM_PROTOCOL_VERSION) {
614         fprintf(stderr, "incompatible version, you are connecting to a ivshmem-"
615                 "server using a different protocol please check your setup\n");
616         qemu_chr_delete(s->server_chr);
617         s->server_chr = NULL;
618         return;
619     }
620
621     IVSHMEM_DPRINTF("version check ok, switch to real chardev handler\n");
622     qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
623                           ivshmem_event, s);
624 }
625
626 /* Select the MSI-X vectors used by device.
627  * ivshmem maps events to vectors statically, so
628  * we just enable all vectors on init and after reset. */
629 static void ivshmem_use_msix(IVShmemState * s)
630 {
631     PCIDevice *d = PCI_DEVICE(s);
632     int i;
633
634     IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__, msix_present(d));
635     if (!msix_present(d)) {
636         return;
637     }
638
639     for (i = 0; i < s->vectors; i++) {
640         msix_vector_use(d, i);
641     }
642 }
643
644 static void ivshmem_reset(DeviceState *d)
645 {
646     IVShmemState *s = IVSHMEM(d);
647
648     s->intrstatus = 0;
649     s->intrmask = 0;
650     ivshmem_use_msix(s);
651 }
652
653 static int ivshmem_setup_msi(IVShmemState * s)
654 {
655     if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
656         return -1;
657     }
658
659     IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
660
661     /* allocate QEMU char devices for receiving interrupts */
662     s->msi_vectors = g_malloc0(s->vectors * sizeof(MSIVector));
663
664     ivshmem_use_msix(s);
665     return 0;
666 }
667
668 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
669                                  uint32_t val, int len)
670 {
671     pci_default_write_config(pci_dev, address, val, len);
672 }
673
674 static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
675 {
676     IVShmemState *s = IVSHMEM(dev);
677     uint8_t *pci_conf;
678     uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
679         PCI_BASE_ADDRESS_MEM_PREFETCH;
680
681     if (!!s->server_chr + !!s->shmobj + !!s->hostmem != 1) {
682         error_setg(errp, "You must specify either a shmobj, a chardev"
683                    " or a hostmem");
684         return;
685     }
686
687     if (s->hostmem) {
688         MemoryRegion *mr;
689
690         if (s->sizearg) {
691             g_warning("size argument ignored with hostmem");
692         }
693
694         mr = host_memory_backend_get_memory(s->hostmem, errp);
695         s->ivshmem_size = memory_region_size(mr);
696     } else if (s->sizearg == NULL) {
697         s->ivshmem_size = 4 << 20; /* 4 MB default */
698     } else {
699         char *end;
700         int64_t size = qemu_strtosz(s->sizearg, &end);
701         if (size < 0 || *end != '\0' || !is_power_of_2(size)) {
702             error_setg(errp, "Invalid size %s", s->sizearg);
703             return;
704         }
705         s->ivshmem_size = size;
706     }
707
708     fifo8_create(&s->incoming_fifo, sizeof(long));
709
710     /* IRQFD requires MSI */
711     if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
712         !ivshmem_has_feature(s, IVSHMEM_MSI)) {
713         error_setg(errp, "ioeventfd/irqfd requires MSI");
714         return;
715     }
716
717     /* check that role is reasonable */
718     if (s->role) {
719         if (strncmp(s->role, "peer", 5) == 0) {
720             s->role_val = IVSHMEM_PEER;
721         } else if (strncmp(s->role, "master", 7) == 0) {
722             s->role_val = IVSHMEM_MASTER;
723         } else {
724             error_setg(errp, "'role' must be 'peer' or 'master'");
725             return;
726         }
727     } else {
728         s->role_val = IVSHMEM_MASTER; /* default */
729     }
730
731     if (s->role_val == IVSHMEM_PEER) {
732         error_setg(&s->migration_blocker,
733                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
734         migrate_add_blocker(s->migration_blocker);
735     }
736
737     pci_conf = dev->config;
738     pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
739
740     pci_config_set_interrupt_pin(pci_conf, 1);
741
742     memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
743                           "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
744
745     /* region for registers*/
746     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
747                      &s->ivshmem_mmio);
748
749     memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
750     if (s->ivshmem_64bit) {
751         attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
752     }
753
754     if (s->hostmem != NULL) {
755         MemoryRegion *mr;
756
757         IVSHMEM_DPRINTF("using hostmem\n");
758
759         mr = host_memory_backend_get_memory(MEMORY_BACKEND(s->hostmem), errp);
760         vmstate_register_ram(mr, DEVICE(s));
761         memory_region_add_subregion(&s->bar, 0, mr);
762         pci_register_bar(PCI_DEVICE(s), 2, attr, &s->bar);
763     } else if (s->server_chr != NULL) {
764         if (strncmp(s->server_chr->filename, "unix:", 5)) {
765             error_setg(errp, "chardev is not a unix client socket");
766             return;
767         }
768
769         /* if we get a UNIX socket as the parameter we will talk
770          * to the ivshmem server to receive the memory region */
771
772         IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
773                         s->server_chr->filename);
774
775         if (ivshmem_has_feature(s, IVSHMEM_MSI) &&
776             ivshmem_setup_msi(s)) {
777             error_setg(errp, "msix initialization failed");
778             return;
779         }
780
781         /* we allocate enough space for 16 peers and grow as needed */
782         resize_peers(s, 16);
783         s->vm_id = -1;
784
785         pci_register_bar(dev, 2, attr, &s->bar);
786
787         s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
788
789         qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive,
790                               ivshmem_check_version, ivshmem_event, s);
791     } else {
792         /* just map the file immediately, we're not using a server */
793         int fd;
794
795         IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
796
797         /* try opening with O_EXCL and if it succeeds zero the memory
798          * by truncating to 0 */
799         if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
800                         S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
801            /* truncate file to length PCI device's memory */
802             if (ftruncate(fd, s->ivshmem_size) != 0) {
803                 error_report("could not truncate shared file");
804             }
805
806         } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
807                         S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
808             error_setg(errp, "could not open shared file");
809             return;
810         }
811
812         if (check_shm_size(s, fd, errp) == -1) {
813             return;
814         }
815
816         create_shared_memory_BAR(s, fd, attr, errp);
817         close(fd);
818     }
819 }
820
821 static void pci_ivshmem_exit(PCIDevice *dev)
822 {
823     IVShmemState *s = IVSHMEM(dev);
824     int i;
825
826     fifo8_destroy(&s->incoming_fifo);
827
828     if (s->migration_blocker) {
829         migrate_del_blocker(s->migration_blocker);
830         error_free(s->migration_blocker);
831     }
832
833     if (memory_region_is_mapped(&s->ivshmem)) {
834         if (!s->hostmem) {
835             void *addr = memory_region_get_ram_ptr(&s->ivshmem);
836
837             if (munmap(addr, s->ivshmem_size) == -1) {
838                 error_report("Failed to munmap shared memory %s",
839                              strerror(errno));
840             }
841         }
842
843         vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
844         memory_region_del_subregion(&s->bar, &s->ivshmem);
845     }
846
847     if (s->eventfd_chr) {
848         for (i = 0; i < s->vectors; i++) {
849             if (s->eventfd_chr[i]) {
850                 qemu_chr_free(s->eventfd_chr[i]);
851             }
852         }
853         g_free(s->eventfd_chr);
854     }
855
856     if (s->peers) {
857         for (i = 0; i < s->nb_peers; i++) {
858             close_peer_eventfds(s, i);
859         }
860         g_free(s->peers);
861     }
862
863     if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
864         msix_uninit_exclusive_bar(dev);
865     }
866
867     g_free(s->msi_vectors);
868 }
869
870 static bool test_msix(void *opaque, int version_id)
871 {
872     IVShmemState *s = opaque;
873
874     return ivshmem_has_feature(s, IVSHMEM_MSI);
875 }
876
877 static bool test_no_msix(void *opaque, int version_id)
878 {
879     return !test_msix(opaque, version_id);
880 }
881
882 static int ivshmem_pre_load(void *opaque)
883 {
884     IVShmemState *s = opaque;
885
886     if (s->role_val == IVSHMEM_PEER) {
887         error_report("'peer' devices are not migratable");
888         return -EINVAL;
889     }
890
891     return 0;
892 }
893
894 static int ivshmem_post_load(void *opaque, int version_id)
895 {
896     IVShmemState *s = opaque;
897
898     if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
899         ivshmem_use_msix(s);
900     }
901
902     return 0;
903 }
904
905 static int ivshmem_load_old(QEMUFile *f, void *opaque, int version_id)
906 {
907     IVShmemState *s = opaque;
908     PCIDevice *pdev = PCI_DEVICE(s);
909     int ret;
910
911     IVSHMEM_DPRINTF("ivshmem_load_old\n");
912
913     if (version_id != 0) {
914         return -EINVAL;
915     }
916
917     if (s->role_val == IVSHMEM_PEER) {
918         error_report("'peer' devices are not migratable");
919         return -EINVAL;
920     }
921
922     ret = pci_device_load(pdev, f);
923     if (ret) {
924         return ret;
925     }
926
927     if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
928         msix_load(pdev, f);
929         ivshmem_use_msix(s);
930     } else {
931         s->intrstatus = qemu_get_be32(f);
932         s->intrmask = qemu_get_be32(f);
933     }
934
935     return 0;
936 }
937
938 static const VMStateDescription ivshmem_vmsd = {
939     .name = "ivshmem",
940     .version_id = 1,
941     .minimum_version_id = 1,
942     .pre_load = ivshmem_pre_load,
943     .post_load = ivshmem_post_load,
944     .fields = (VMStateField[]) {
945         VMSTATE_PCI_DEVICE(parent_obj, IVShmemState),
946
947         VMSTATE_MSIX_TEST(parent_obj, IVShmemState, test_msix),
948         VMSTATE_UINT32_TEST(intrstatus, IVShmemState, test_no_msix),
949         VMSTATE_UINT32_TEST(intrmask, IVShmemState, test_no_msix),
950
951         VMSTATE_END_OF_LIST()
952     },
953     .load_state_old = ivshmem_load_old,
954     .minimum_version_id_old = 0
955 };
956
957 static Property ivshmem_properties[] = {
958     DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
959     DEFINE_PROP_STRING("size", IVShmemState, sizearg),
960     DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
961     DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
962     DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
963     DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
964     DEFINE_PROP_STRING("role", IVShmemState, role),
965     DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
966     DEFINE_PROP_END_OF_LIST(),
967 };
968
969 static void ivshmem_class_init(ObjectClass *klass, void *data)
970 {
971     DeviceClass *dc = DEVICE_CLASS(klass);
972     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
973
974     k->realize = pci_ivshmem_realize;
975     k->exit = pci_ivshmem_exit;
976     k->config_write = ivshmem_write_config;
977     k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
978     k->device_id = PCI_DEVICE_ID_IVSHMEM;
979     k->class_id = PCI_CLASS_MEMORY_RAM;
980     dc->reset = ivshmem_reset;
981     dc->props = ivshmem_properties;
982     dc->vmsd = &ivshmem_vmsd;
983     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
984     dc->desc = "Inter-VM shared memory";
985 }
986
987 static void ivshmem_check_memdev_is_busy(Object *obj, const char *name,
988                                          Object *val, Error **errp)
989 {
990     MemoryRegion *mr;
991
992     mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp);
993     if (memory_region_is_mapped(mr)) {
994         char *path = object_get_canonical_path_component(val);
995         error_setg(errp, "can't use already busy memdev: %s", path);
996         g_free(path);
997     } else {
998         qdev_prop_allow_set_link_before_realize(obj, name, val, errp);
999     }
1000 }
1001
1002 static void ivshmem_init(Object *obj)
1003 {
1004     IVShmemState *s = IVSHMEM(obj);
1005
1006     object_property_add_link(obj, IVSHMEM_MEMDEV_PROP, TYPE_MEMORY_BACKEND,
1007                              (Object **)&s->hostmem,
1008                              ivshmem_check_memdev_is_busy,
1009                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
1010                              &error_abort);
1011 }
1012
1013 static const TypeInfo ivshmem_info = {
1014     .name          = TYPE_IVSHMEM,
1015     .parent        = TYPE_PCI_DEVICE,
1016     .instance_size = sizeof(IVShmemState),
1017     .instance_init = ivshmem_init,
1018     .class_init    = ivshmem_class_init,
1019 };
1020
1021 static void ivshmem_register_types(void)
1022 {
1023     type_register_static(&ivshmem_info);
1024 }
1025
1026 type_init(ivshmem_register_types)
This page took 0.078135 seconds and 4 git commands to generate.