]> Git Repo - qemu.git/blob - hw/misc/ivshmem.c
ivshmem: simplify a bit the code
[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
30 #include <sys/mman.h>
31 #include <sys/types.h>
32 #include <limits.h>
33
34 #define PCI_VENDOR_ID_IVSHMEM   PCI_VENDOR_ID_REDHAT_QUMRANET
35 #define PCI_DEVICE_ID_IVSHMEM   0x1110
36
37 #define IVSHMEM_MAX_PEERS G_MAXUINT16
38 #define IVSHMEM_IOEVENTFD   0
39 #define IVSHMEM_MSI     1
40
41 #define IVSHMEM_PEER    0
42 #define IVSHMEM_MASTER  1
43
44 #define IVSHMEM_REG_BAR_SIZE 0x100
45
46 //#define DEBUG_IVSHMEM
47 #ifdef DEBUG_IVSHMEM
48 #define IVSHMEM_DPRINTF(fmt, ...)        \
49     do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
50 #else
51 #define IVSHMEM_DPRINTF(fmt, ...)
52 #endif
53
54 #define TYPE_IVSHMEM "ivshmem"
55 #define IVSHMEM(obj) \
56     OBJECT_CHECK(IVShmemState, (obj), TYPE_IVSHMEM)
57
58 typedef struct Peer {
59     int nb_eventfds;
60     EventNotifier *eventfds;
61 } Peer;
62
63 typedef struct EventfdEntry {
64     PCIDevice *pdev;
65     int vector;
66 } EventfdEntry;
67
68 typedef struct IVShmemState {
69     /*< private >*/
70     PCIDevice parent_obj;
71     /*< public >*/
72
73     uint32_t intrmask;
74     uint32_t intrstatus;
75
76     CharDriverState **eventfd_chr;
77     CharDriverState *server_chr;
78     Fifo8 incoming_fifo;
79     MemoryRegion ivshmem_mmio;
80
81     /* We might need to register the BAR before we actually have the memory.
82      * So prepare a container MemoryRegion for the BAR immediately and
83      * add a subregion when we have the memory.
84      */
85     MemoryRegion bar;
86     MemoryRegion ivshmem;
87     uint64_t ivshmem_size; /* size of shared memory region */
88     uint32_t ivshmem_64bit;
89     int shm_fd; /* shared memory file descriptor */
90
91     Peer *peers;
92     int nb_peers; /* how many guests we have space for */
93
94     int vm_id;
95     uint32_t vectors;
96     uint32_t features;
97     EventfdEntry *eventfd_table;
98
99     Error *migration_blocker;
100
101     char * shmobj;
102     char * sizearg;
103     char * role;
104     int role_val;   /* scalar to avoid multiple string comparisons */
105 } IVShmemState;
106
107 /* registers for the Inter-VM shared memory device */
108 enum ivshmem_registers {
109     INTRMASK = 0,
110     INTRSTATUS = 4,
111     IVPOSITION = 8,
112     DOORBELL = 12,
113 };
114
115 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
116                                                     unsigned int feature) {
117     return (ivs->features & (1 << feature));
118 }
119
120 static inline bool is_power_of_two(uint64_t x) {
121     return (x & (x - 1)) == 0;
122 }
123
124 /* accessing registers - based on rtl8139 */
125 static void ivshmem_update_irq(IVShmemState *s)
126 {
127     PCIDevice *d = PCI_DEVICE(s);
128     int isr;
129     isr = (s->intrstatus & s->intrmask) & 0xffffffff;
130
131     /* don't print ISR resets */
132     if (isr) {
133         IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
134                         isr ? 1 : 0, s->intrstatus, s->intrmask);
135     }
136
137     pci_set_irq(d, (isr != 0));
138 }
139
140 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
141 {
142     IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
143
144     s->intrmask = val;
145
146     ivshmem_update_irq(s);
147 }
148
149 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
150 {
151     uint32_t ret = s->intrmask;
152
153     IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
154
155     return ret;
156 }
157
158 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
159 {
160     IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
161
162     s->intrstatus = val;
163
164     ivshmem_update_irq(s);
165 }
166
167 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
168 {
169     uint32_t ret = s->intrstatus;
170
171     /* reading ISR clears all interrupts */
172     s->intrstatus = 0;
173
174     ivshmem_update_irq(s);
175
176     return ret;
177 }
178
179 static void ivshmem_io_write(void *opaque, hwaddr addr,
180                              uint64_t val, unsigned size)
181 {
182     IVShmemState *s = opaque;
183
184     uint16_t dest = val >> 16;
185     uint16_t vector = val & 0xff;
186
187     addr &= 0xfc;
188
189     IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
190     switch (addr)
191     {
192         case INTRMASK:
193             ivshmem_IntrMask_write(s, val);
194             break;
195
196         case INTRSTATUS:
197             ivshmem_IntrStatus_write(s, val);
198             break;
199
200         case DOORBELL:
201             /* check that dest VM ID is reasonable */
202             if (dest >= s->nb_peers) {
203                 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
204                 break;
205             }
206
207             /* check doorbell range */
208             if (vector < s->peers[dest].nb_eventfds) {
209                 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
210                 event_notifier_set(&s->peers[dest].eventfds[vector]);
211             } else {
212                 IVSHMEM_DPRINTF("Invalid destination vector %d on VM %d\n",
213                                 vector, dest);
214             }
215             break;
216         default:
217             IVSHMEM_DPRINTF("Unhandled write " TARGET_FMT_plx "\n", addr);
218     }
219 }
220
221 static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
222                                 unsigned size)
223 {
224
225     IVShmemState *s = opaque;
226     uint32_t ret;
227
228     switch (addr)
229     {
230         case INTRMASK:
231             ret = ivshmem_IntrMask_read(s);
232             break;
233
234         case INTRSTATUS:
235             ret = ivshmem_IntrStatus_read(s);
236             break;
237
238         case IVPOSITION:
239             /* return my VM ID if the memory is mapped */
240             if (s->shm_fd > 0) {
241                 ret = s->vm_id;
242             } else {
243                 ret = -1;
244             }
245             break;
246
247         default:
248             IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
249             ret = 0;
250     }
251
252     return ret;
253 }
254
255 static const MemoryRegionOps ivshmem_mmio_ops = {
256     .read = ivshmem_io_read,
257     .write = ivshmem_io_write,
258     .endianness = DEVICE_NATIVE_ENDIAN,
259     .impl = {
260         .min_access_size = 4,
261         .max_access_size = 4,
262     },
263 };
264
265 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
266 {
267     IVShmemState *s = opaque;
268
269     IVSHMEM_DPRINTF("ivshmem_receive 0x%02x size: %d\n", *buf, size);
270
271     ivshmem_IntrStatus_write(s, *buf);
272 }
273
274 static int ivshmem_can_receive(void * opaque)
275 {
276     return sizeof(long);
277 }
278
279 static void ivshmem_event(void *opaque, int event)
280 {
281     IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
282 }
283
284 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
285
286     EventfdEntry *entry = opaque;
287     PCIDevice *pdev = entry->pdev;
288
289     IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
290     msix_notify(pdev, entry->vector);
291 }
292
293 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
294                                                   int vector)
295 {
296     /* create a event character device based on the passed eventfd */
297     IVShmemState *s = opaque;
298     CharDriverState * chr;
299     int eventfd = event_notifier_get_fd(n);
300
301     chr = qemu_chr_open_eventfd(eventfd);
302
303     if (chr == NULL) {
304         error_report("creating chardriver for eventfd %d failed", eventfd);
305         return NULL;
306     }
307     qemu_chr_fe_claim_no_fail(chr);
308
309     /* if MSI is supported we need multiple interrupts */
310     if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
311         s->eventfd_table[vector].pdev = PCI_DEVICE(s);
312         s->eventfd_table[vector].vector = vector;
313
314         qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
315                       ivshmem_event, &s->eventfd_table[vector]);
316     } else {
317         qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
318                       ivshmem_event, s);
319     }
320
321     return chr;
322
323 }
324
325 static int check_shm_size(IVShmemState *s, int fd, Error **errp)
326 {
327     /* check that the guest isn't going to try and map more memory than the
328      * the object has allocated return -1 to indicate error */
329
330     struct stat buf;
331
332     if (fstat(fd, &buf) < 0) {
333         error_setg(errp, "exiting: fstat on fd %d failed: %s",
334                    fd, strerror(errno));
335         return -1;
336     }
337
338     if (s->ivshmem_size > buf.st_size) {
339         error_setg(errp, "Requested memory size greater"
340                    " than shared object size (%" PRIu64 " > %" PRIu64")",
341                    s->ivshmem_size, (uint64_t)buf.st_size);
342         return -1;
343     } else {
344         return 0;
345     }
346 }
347
348 /* create the shared memory BAR when we are not using the server, so we can
349  * create the BAR and map the memory immediately */
350 static int create_shared_memory_BAR(IVShmemState *s, int fd, uint8_t attr,
351                                     Error **errp)
352 {
353     void * ptr;
354
355     ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
356     if (ptr == MAP_FAILED) {
357         error_setg_errno(errp, errno, "Failed to mmap shared memory");
358         return -1;
359     }
360
361     s->shm_fd = fd;
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_guest_eventfds(IVShmemState *s, int posn)
395 {
396     int i, guest_curr_max;
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     guest_curr_max = s->peers[posn].nb_eventfds;
407
408     memory_region_transaction_begin();
409     for (i = 0; i < guest_curr_max; i++) {
410         ivshmem_del_eventfd(s, posn, i);
411     }
412     memory_region_transaction_commit();
413     for (i = 0; i < guest_curr_max; 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  * guests */
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 guests\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 guest */
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             return;
529         } else {
530             /* otherwise an fd == -1 means an existing guest has gone away */
531             IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
532             close_guest_eventfds(s, incoming_posn);
533             return;
534         }
535     }
536
537     /* if the position is -1, then it's shared memory region fd */
538     if (incoming_posn == -1) {
539         void * map_ptr;
540
541         if (check_shm_size(s, incoming_fd, &err) == -1) {
542             error_report_err(err);
543             close(incoming_fd);
544             return;
545         }
546
547         /* mmap the region and map into the BAR2 */
548         map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
549                                                             incoming_fd, 0);
550         if (map_ptr == MAP_FAILED) {
551             error_report("Failed to mmap shared memory %s", strerror(errno));
552             close(incoming_fd);
553             return;
554         }
555         memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
556                                    "ivshmem.bar2", s->ivshmem_size, map_ptr);
557         vmstate_register_ram(&s->ivshmem, DEVICE(s));
558
559         IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",
560                         map_ptr, s->ivshmem_size);
561
562         memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
563
564         /* only store the fd if it is successfully mapped */
565         s->shm_fd = incoming_fd;
566
567         return;
568     }
569
570     /* each peer has an associated array of eventfds, and we keep
571      * track of how many eventfds received so far */
572     /* get a new eventfd: */
573     new_eventfd = peer->nb_eventfds++;
574
575     /* this is an eventfd for a particular guest VM */
576     IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
577                     new_eventfd, incoming_fd);
578     event_notifier_init_fd(&peer->eventfds[new_eventfd], incoming_fd);
579
580     if (incoming_posn == s->vm_id) {
581         s->eventfd_chr[new_eventfd] = create_eventfd_chr_device(s,
582                    &s->peers[s->vm_id].eventfds[new_eventfd],
583                    new_eventfd);
584     }
585
586     if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
587         ivshmem_add_eventfd(s, incoming_posn, new_eventfd);
588     }
589 }
590
591 /* Select the MSI-X vectors used by device.
592  * ivshmem maps events to vectors statically, so
593  * we just enable all vectors on init and after reset. */
594 static void ivshmem_use_msix(IVShmemState * s)
595 {
596     PCIDevice *d = PCI_DEVICE(s);
597     int i;
598
599     IVSHMEM_DPRINTF("%s, msix present: %d\n", __func__, msix_present(d));
600     if (!msix_present(d)) {
601         return;
602     }
603
604     for (i = 0; i < s->vectors; i++) {
605         msix_vector_use(d, i);
606     }
607 }
608
609 static void ivshmem_reset(DeviceState *d)
610 {
611     IVShmemState *s = IVSHMEM(d);
612
613     s->intrstatus = 0;
614     ivshmem_use_msix(s);
615 }
616
617 static uint64_t ivshmem_get_size(IVShmemState * s, Error **errp) {
618
619     uint64_t value;
620     char *ptr;
621
622     value = strtoull(s->sizearg, &ptr, 10);
623     switch (*ptr) {
624         case 0: case 'M': case 'm':
625             value <<= 20;
626             break;
627         case 'G': case 'g':
628             value <<= 30;
629             break;
630         default:
631             error_setg(errp, "invalid ram size: %s", s->sizearg);
632             return 0;
633     }
634
635     /* BARs must be a power of 2 */
636     if (!is_power_of_two(value)) {
637         error_setg(errp, "size must be power of 2");
638         return 0;
639     }
640
641     return value;
642 }
643
644 static int ivshmem_setup_msi(IVShmemState * s)
645 {
646     if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
647         return -1;
648     }
649
650     IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
651
652     /* allocate QEMU char devices for receiving interrupts */
653     s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
654
655     ivshmem_use_msix(s);
656     return 0;
657 }
658
659 static void ivshmem_save(QEMUFile* f, void *opaque)
660 {
661     IVShmemState *proxy = opaque;
662     PCIDevice *pci_dev = PCI_DEVICE(proxy);
663
664     IVSHMEM_DPRINTF("ivshmem_save\n");
665     pci_device_save(pci_dev, f);
666
667     if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
668         msix_save(pci_dev, f);
669     } else {
670         qemu_put_be32(f, proxy->intrstatus);
671         qemu_put_be32(f, proxy->intrmask);
672     }
673
674 }
675
676 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
677 {
678     IVSHMEM_DPRINTF("ivshmem_load\n");
679
680     IVShmemState *proxy = opaque;
681     PCIDevice *pci_dev = PCI_DEVICE(proxy);
682     int ret;
683
684     if (version_id > 0) {
685         return -EINVAL;
686     }
687
688     if (proxy->role_val == IVSHMEM_PEER) {
689         error_report("'peer' devices are not migratable");
690         return -EINVAL;
691     }
692
693     ret = pci_device_load(pci_dev, f);
694     if (ret) {
695         return ret;
696     }
697
698     if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
699         msix_load(pci_dev, f);
700         ivshmem_use_msix(proxy);
701     } else {
702         proxy->intrstatus = qemu_get_be32(f);
703         proxy->intrmask = qemu_get_be32(f);
704     }
705
706     return 0;
707 }
708
709 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
710                                  uint32_t val, int len)
711 {
712     pci_default_write_config(pci_dev, address, val, len);
713 }
714
715 static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
716 {
717     IVShmemState *s = IVSHMEM(dev);
718     uint8_t *pci_conf;
719     uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
720         PCI_BASE_ADDRESS_MEM_PREFETCH;
721     Error *local_err = NULL;
722
723     if (s->sizearg == NULL) {
724         s->ivshmem_size = 4 << 20; /* 4 MB default */
725     } else {
726         s->ivshmem_size = ivshmem_get_size(s, &local_err);
727         if (local_err) {
728             error_propagate(errp, local_err);
729             return;
730         }
731     }
732
733     fifo8_create(&s->incoming_fifo, sizeof(long));
734     register_savevm(DEVICE(dev), "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
735                                                                         dev);
736     /* IRQFD requires MSI */
737     if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
738         !ivshmem_has_feature(s, IVSHMEM_MSI)) {
739         error_setg(errp, "ioeventfd/irqfd requires MSI");
740         return;
741     }
742
743     /* check that role is reasonable */
744     if (s->role) {
745         if (strncmp(s->role, "peer", 5) == 0) {
746             s->role_val = IVSHMEM_PEER;
747         } else if (strncmp(s->role, "master", 7) == 0) {
748             s->role_val = IVSHMEM_MASTER;
749         } else {
750             error_setg(errp, "'role' must be 'peer' or 'master'");
751             return;
752         }
753     } else {
754         s->role_val = IVSHMEM_MASTER; /* default */
755     }
756
757     if (s->role_val == IVSHMEM_PEER) {
758         error_setg(&s->migration_blocker,
759                    "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
760         migrate_add_blocker(s->migration_blocker);
761     }
762
763     pci_conf = dev->config;
764     pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
765
766     pci_config_set_interrupt_pin(pci_conf, 1);
767
768     s->shm_fd = 0;
769
770     memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
771                           "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
772
773     /* region for registers*/
774     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
775                      &s->ivshmem_mmio);
776
777     memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
778     if (s->ivshmem_64bit) {
779         attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
780     }
781
782     if (s->server_chr != NULL) {
783         if (strncmp(s->server_chr->filename, "unix:", 5)) {
784             error_setg(errp, "chardev is not a unix client socket");
785             return;
786         }
787
788         /* if we get a UNIX socket as the parameter we will talk
789          * to the ivshmem server to receive the memory region */
790
791         if (s->shmobj != NULL) {
792             error_setg(errp, "do not specify both 'chardev' "
793                        "and 'shm' with ivshmem");
794             return;
795         }
796
797         IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
798                         s->server_chr->filename);
799
800         if (ivshmem_has_feature(s, IVSHMEM_MSI) &&
801             ivshmem_setup_msi(s)) {
802             error_setg(errp, "msix initialization failed");
803             return;
804         }
805
806         /* we allocate enough space for 16 guests and grow as needed */
807         resize_peers(s, 16);
808         s->vm_id = -1;
809
810         pci_register_bar(dev, 2, attr, &s->bar);
811
812         s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
813
814         qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
815                      ivshmem_event, s);
816     } else {
817         /* just map the file immediately, we're not using a server */
818         int fd;
819
820         if (s->shmobj == NULL) {
821             error_setg(errp, "Must specify 'chardev' or 'shm' to ivshmem");
822             return;
823         }
824
825         IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
826
827         /* try opening with O_EXCL and if it succeeds zero the memory
828          * by truncating to 0 */
829         if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
830                         S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
831            /* truncate file to length PCI device's memory */
832             if (ftruncate(fd, s->ivshmem_size) != 0) {
833                 error_report("could not truncate shared file");
834             }
835
836         } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
837                         S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
838             error_setg(errp, "could not open shared file");
839             return;
840         }
841
842         if (check_shm_size(s, fd, errp) == -1) {
843             return;
844         }
845
846         create_shared_memory_BAR(s, fd, attr, errp);
847     }
848 }
849
850 static void pci_ivshmem_exit(PCIDevice *dev)
851 {
852     IVShmemState *s = IVSHMEM(dev);
853
854     if (s->migration_blocker) {
855         migrate_del_blocker(s->migration_blocker);
856         error_free(s->migration_blocker);
857     }
858
859     memory_region_del_subregion(&s->bar, &s->ivshmem);
860     vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
861     unregister_savevm(DEVICE(dev), "ivshmem", s);
862     fifo8_destroy(&s->incoming_fifo);
863 }
864
865 static Property ivshmem_properties[] = {
866     DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
867     DEFINE_PROP_STRING("size", IVShmemState, sizearg),
868     DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
869     DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
870     DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
871     DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
872     DEFINE_PROP_STRING("role", IVShmemState, role),
873     DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
874     DEFINE_PROP_END_OF_LIST(),
875 };
876
877 static void ivshmem_class_init(ObjectClass *klass, void *data)
878 {
879     DeviceClass *dc = DEVICE_CLASS(klass);
880     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
881
882     k->realize = pci_ivshmem_realize;
883     k->exit = pci_ivshmem_exit;
884     k->config_write = ivshmem_write_config;
885     k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
886     k->device_id = PCI_DEVICE_ID_IVSHMEM;
887     k->class_id = PCI_CLASS_MEMORY_RAM;
888     dc->reset = ivshmem_reset;
889     dc->props = ivshmem_properties;
890     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
891 }
892
893 static const TypeInfo ivshmem_info = {
894     .name          = TYPE_IVSHMEM,
895     .parent        = TYPE_PCI_DEVICE,
896     .instance_size = sizeof(IVShmemState),
897     .class_init    = ivshmem_class_init,
898 };
899
900 static void ivshmem_register_types(void)
901 {
902     type_register_static(&ivshmem_info);
903 }
904
905 type_init(ivshmem_register_types)
This page took 0.071981 seconds and 4 git commands to generate.