]> git.proxmox.com Git - mirror_qemu.git/blob - hw/misc/ivshmem.c
qerror: Move #include out of qerror.h
[mirror_qemu.git] / hw / misc / ivshmem.c
1 /*
2 * Inter-VM Shared Memory PCI device.
3 *
4 * Author:
5 * Cam Macdonell <cam@cs.ualberta.ca>
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 "qapi/qmp/qerror.h"
26 #include "qemu/error-report.h"
27 #include "qemu/event_notifier.h"
28 #include "qemu/fifo8.h"
29 #include "sysemu/char.h"
30
31 #include <sys/mman.h>
32 #include <sys/types.h>
33 #include <limits.h>
34
35 #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
36 #define PCI_DEVICE_ID_IVSHMEM 0x1110
37
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 uint32_t doorbell;
76
77 CharDriverState **eventfd_chr;
78 CharDriverState *server_chr;
79 Fifo8 incoming_fifo;
80 MemoryRegion ivshmem_mmio;
81
82 /* We might need to register the BAR before we actually have the memory.
83 * So prepare a container MemoryRegion for the BAR immediately and
84 * add a subregion when we have the memory.
85 */
86 MemoryRegion bar;
87 MemoryRegion ivshmem;
88 uint64_t ivshmem_size; /* size of shared memory region */
89 uint32_t ivshmem_attr;
90 uint32_t ivshmem_64bit;
91 int shm_fd; /* shared memory file descriptor */
92
93 Peer *peers;
94 int nb_peers; /* how many guests we have space for */
95 int max_peer; /* maximum numbered peer */
96
97 int vm_id;
98 uint32_t vectors;
99 uint32_t features;
100 EventfdEntry *eventfd_table;
101
102 Error *migration_blocker;
103
104 char * shmobj;
105 char * sizearg;
106 char * role;
107 int role_val; /* scalar to avoid multiple string comparisons */
108 } IVShmemState;
109
110 /* registers for the Inter-VM shared memory device */
111 enum ivshmem_registers {
112 INTRMASK = 0,
113 INTRSTATUS = 4,
114 IVPOSITION = 8,
115 DOORBELL = 12,
116 };
117
118 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
119 unsigned int feature) {
120 return (ivs->features & (1 << feature));
121 }
122
123 static inline bool is_power_of_two(uint64_t x) {
124 return (x & (x - 1)) == 0;
125 }
126
127 /* accessing registers - based on rtl8139 */
128 static void ivshmem_update_irq(IVShmemState *s, int val)
129 {
130 PCIDevice *d = PCI_DEVICE(s);
131 int isr;
132 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
133
134 /* don't print ISR resets */
135 if (isr) {
136 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
137 isr ? 1 : 0, s->intrstatus, s->intrmask);
138 }
139
140 pci_set_irq(d, (isr != 0));
141 }
142
143 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
144 {
145 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
146
147 s->intrmask = val;
148
149 ivshmem_update_irq(s, val);
150 }
151
152 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
153 {
154 uint32_t ret = s->intrmask;
155
156 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
157
158 return ret;
159 }
160
161 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
162 {
163 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
164
165 s->intrstatus = val;
166
167 ivshmem_update_irq(s, val);
168 }
169
170 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
171 {
172 uint32_t ret = s->intrstatus;
173
174 /* reading ISR clears all interrupts */
175 s->intrstatus = 0;
176
177 ivshmem_update_irq(s, 0);
178
179 return ret;
180 }
181
182 static void ivshmem_io_write(void *opaque, hwaddr addr,
183 uint64_t val, unsigned size)
184 {
185 IVShmemState *s = opaque;
186
187 uint16_t dest = val >> 16;
188 uint16_t vector = val & 0xff;
189
190 addr &= 0xfc;
191
192 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
193 switch (addr)
194 {
195 case INTRMASK:
196 ivshmem_IntrMask_write(s, val);
197 break;
198
199 case INTRSTATUS:
200 ivshmem_IntrStatus_write(s, val);
201 break;
202
203 case DOORBELL:
204 /* check that dest VM ID is reasonable */
205 if (dest > s->max_peer) {
206 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
207 break;
208 }
209
210 /* check doorbell range */
211 if (vector < s->peers[dest].nb_eventfds) {
212 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
213 event_notifier_set(&s->peers[dest].eventfds[vector]);
214 }
215 break;
216 default:
217 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
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_IntrStatus_write(s, *buf);
270
271 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
272 }
273
274 static int ivshmem_can_receive(void * opaque)
275 {
276 return 8;
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 eventfd for eventfd %d failed", eventfd);
305 exit(1);
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) {
326 /* check that the guest isn't going to try and map more memory than the
327 * the object has allocated return -1 to indicate error */
328
329 struct stat buf;
330
331 if (fstat(fd, &buf) < 0) {
332 error_report("exiting: fstat on fd %d failed: %s",
333 fd, strerror(errno));
334 return -1;
335 }
336
337 if (s->ivshmem_size > buf.st_size) {
338 error_report("Requested memory size greater"
339 " than shared object size (%" PRIu64 " > %" PRIu64")",
340 s->ivshmem_size, (uint64_t)buf.st_size);
341 return -1;
342 } else {
343 return 0;
344 }
345 }
346
347 /* create the shared memory BAR when we are not using the server, so we can
348 * create the BAR and map the memory immediately */
349 static void create_shared_memory_BAR(IVShmemState *s, int fd) {
350
351 void * ptr;
352
353 s->shm_fd = fd;
354
355 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
356
357 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), "ivshmem.bar2",
358 s->ivshmem_size, ptr);
359 vmstate_register_ram(&s->ivshmem, DEVICE(s));
360 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
361
362 /* region for shared memory */
363 pci_register_bar(PCI_DEVICE(s), 2, s->ivshmem_attr, &s->bar);
364 }
365
366 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
367 {
368 memory_region_add_eventfd(&s->ivshmem_mmio,
369 DOORBELL,
370 4,
371 true,
372 (posn << 16) | i,
373 &s->peers[posn].eventfds[i]);
374 }
375
376 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
377 {
378 memory_region_del_eventfd(&s->ivshmem_mmio,
379 DOORBELL,
380 4,
381 true,
382 (posn << 16) | i,
383 &s->peers[posn].eventfds[i]);
384 }
385
386 static void close_guest_eventfds(IVShmemState *s, int posn)
387 {
388 int i, guest_curr_max;
389
390 if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
391 return;
392 }
393 if (posn < 0 || posn >= s->nb_peers) {
394 return;
395 }
396
397 guest_curr_max = s->peers[posn].nb_eventfds;
398
399 memory_region_transaction_begin();
400 for (i = 0; i < guest_curr_max; i++) {
401 ivshmem_del_eventfd(s, posn, i);
402 }
403 memory_region_transaction_commit();
404 for (i = 0; i < guest_curr_max; i++) {
405 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
406 }
407
408 g_free(s->peers[posn].eventfds);
409 s->peers[posn].nb_eventfds = 0;
410 }
411
412 /* this function increase the dynamic storage need to store data about other
413 * guests */
414 static int increase_dynamic_storage(IVShmemState *s, int new_min_size)
415 {
416
417 int j, old_nb_alloc;
418
419 /* check for integer overflow */
420 if (new_min_size >= INT_MAX / sizeof(Peer) - 1 || new_min_size <= 0) {
421 return -1;
422 }
423
424 old_nb_alloc = s->nb_peers;
425
426 if (new_min_size >= s->nb_peers) {
427 /* +1 because #new_min_size is used as last array index */
428 s->nb_peers = new_min_size + 1;
429 } else {
430 return 0;
431 }
432
433 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
434 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
435
436 /* zero out new pointers */
437 for (j = old_nb_alloc; j < s->nb_peers; j++) {
438 s->peers[j].eventfds = NULL;
439 s->peers[j].nb_eventfds = 0;
440 }
441
442 return 0;
443 }
444
445 static void ivshmem_read(void *opaque, const uint8_t *buf, int size)
446 {
447 IVShmemState *s = opaque;
448 int incoming_fd, tmp_fd;
449 int guest_max_eventfd;
450 long incoming_posn;
451
452 if (fifo8_is_empty(&s->incoming_fifo) && size == sizeof(incoming_posn)) {
453 memcpy(&incoming_posn, buf, size);
454 } else {
455 const uint8_t *p;
456 uint32_t num;
457
458 IVSHMEM_DPRINTF("short read of %d bytes\n", size);
459 num = MAX(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo));
460 fifo8_push_all(&s->incoming_fifo, buf, num);
461 if (fifo8_num_used(&s->incoming_fifo) < sizeof(incoming_posn)) {
462 return;
463 }
464 size -= num;
465 buf += num;
466 p = fifo8_pop_buf(&s->incoming_fifo, sizeof(incoming_posn), &num);
467 g_assert(num == sizeof(incoming_posn));
468 memcpy(&incoming_posn, p, sizeof(incoming_posn));
469 if (size > 0) {
470 fifo8_push_all(&s->incoming_fifo, buf, size);
471 }
472 }
473
474 if (incoming_posn < -1) {
475 IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn);
476 return;
477 }
478
479 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
480 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
481 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
482
483 /* make sure we have enough space for this guest */
484 if (incoming_posn >= s->nb_peers) {
485 if (increase_dynamic_storage(s, incoming_posn) < 0) {
486 error_report("increase_dynamic_storage() failed");
487 if (tmp_fd != -1) {
488 close(tmp_fd);
489 }
490 return;
491 }
492 }
493
494 if (tmp_fd == -1) {
495 /* if posn is positive and unseen before then this is our posn*/
496 if ((incoming_posn >= 0) &&
497 (s->peers[incoming_posn].eventfds == NULL)) {
498 /* receive our posn */
499 s->vm_id = incoming_posn;
500 return;
501 } else {
502 /* otherwise an fd == -1 means an existing guest has gone away */
503 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
504 close_guest_eventfds(s, incoming_posn);
505 return;
506 }
507 }
508
509 /* because of the implementation of get_msgfd, we need a dup */
510 incoming_fd = dup(tmp_fd);
511
512 if (incoming_fd == -1) {
513 error_report("could not allocate file descriptor %s", strerror(errno));
514 close(tmp_fd);
515 return;
516 }
517
518 /* if the position is -1, then it's shared memory region fd */
519 if (incoming_posn == -1) {
520
521 void * map_ptr;
522
523 s->max_peer = 0;
524
525 if (check_shm_size(s, incoming_fd) == -1) {
526 exit(1);
527 }
528
529 /* mmap the region and map into the BAR2 */
530 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
531 incoming_fd, 0);
532 memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
533 "ivshmem.bar2", s->ivshmem_size, map_ptr);
534 vmstate_register_ram(&s->ivshmem, DEVICE(s));
535
536 IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",
537 map_ptr, s->ivshmem_size);
538
539 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
540
541 /* only store the fd if it is successfully mapped */
542 s->shm_fd = incoming_fd;
543
544 return;
545 }
546
547 /* each guest has an array of eventfds, and we keep track of how many
548 * guests for each VM */
549 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
550
551 if (guest_max_eventfd == 0) {
552 /* one eventfd per MSI vector */
553 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
554 }
555
556 /* this is an eventfd for a particular guest VM */
557 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
558 guest_max_eventfd, incoming_fd);
559 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
560 incoming_fd);
561
562 /* increment count for particular guest */
563 s->peers[incoming_posn].nb_eventfds++;
564
565 /* keep track of the maximum VM ID */
566 if (incoming_posn > s->max_peer) {
567 s->max_peer = incoming_posn;
568 }
569
570 if (incoming_posn == s->vm_id) {
571 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
572 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
573 guest_max_eventfd);
574 }
575
576 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
577 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
578 }
579 }
580
581 /* Select the MSI-X vectors used by device.
582 * ivshmem maps events to vectors statically, so
583 * we just enable all vectors on init and after reset. */
584 static void ivshmem_use_msix(IVShmemState * s)
585 {
586 PCIDevice *d = PCI_DEVICE(s);
587 int i;
588
589 if (!msix_present(d)) {
590 return;
591 }
592
593 for (i = 0; i < s->vectors; i++) {
594 msix_vector_use(d, i);
595 }
596 }
597
598 static void ivshmem_reset(DeviceState *d)
599 {
600 IVShmemState *s = IVSHMEM(d);
601
602 s->intrstatus = 0;
603 ivshmem_use_msix(s);
604 }
605
606 static uint64_t ivshmem_get_size(IVShmemState * s) {
607
608 uint64_t value;
609 char *ptr;
610
611 value = strtoull(s->sizearg, &ptr, 10);
612 switch (*ptr) {
613 case 0: case 'M': case 'm':
614 value <<= 20;
615 break;
616 case 'G': case 'g':
617 value <<= 30;
618 break;
619 default:
620 error_report("invalid ram size: %s", s->sizearg);
621 exit(1);
622 }
623
624 /* BARs must be a power of 2 */
625 if (!is_power_of_two(value)) {
626 error_report("size must be power of 2");
627 exit(1);
628 }
629
630 return value;
631 }
632
633 static void ivshmem_setup_msi(IVShmemState * s)
634 {
635 if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
636 IVSHMEM_DPRINTF("msix initialization failed\n");
637 exit(1);
638 }
639
640 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
641
642 /* allocate QEMU char devices for receiving interrupts */
643 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
644
645 ivshmem_use_msix(s);
646 }
647
648 static void ivshmem_save(QEMUFile* f, void *opaque)
649 {
650 IVShmemState *proxy = opaque;
651 PCIDevice *pci_dev = PCI_DEVICE(proxy);
652
653 IVSHMEM_DPRINTF("ivshmem_save\n");
654 pci_device_save(pci_dev, f);
655
656 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
657 msix_save(pci_dev, f);
658 } else {
659 qemu_put_be32(f, proxy->intrstatus);
660 qemu_put_be32(f, proxy->intrmask);
661 }
662
663 }
664
665 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
666 {
667 IVSHMEM_DPRINTF("ivshmem_load\n");
668
669 IVShmemState *proxy = opaque;
670 PCIDevice *pci_dev = PCI_DEVICE(proxy);
671 int ret;
672
673 if (version_id > 0) {
674 return -EINVAL;
675 }
676
677 if (proxy->role_val == IVSHMEM_PEER) {
678 error_report("'peer' devices are not migratable");
679 return -EINVAL;
680 }
681
682 ret = pci_device_load(pci_dev, f);
683 if (ret) {
684 return ret;
685 }
686
687 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
688 msix_load(pci_dev, f);
689 ivshmem_use_msix(proxy);
690 } else {
691 proxy->intrstatus = qemu_get_be32(f);
692 proxy->intrmask = qemu_get_be32(f);
693 }
694
695 return 0;
696 }
697
698 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
699 uint32_t val, int len)
700 {
701 pci_default_write_config(pci_dev, address, val, len);
702 }
703
704 static int pci_ivshmem_init(PCIDevice *dev)
705 {
706 IVShmemState *s = IVSHMEM(dev);
707 uint8_t *pci_conf;
708
709 if (s->sizearg == NULL)
710 s->ivshmem_size = 4 << 20; /* 4 MB default */
711 else {
712 s->ivshmem_size = ivshmem_get_size(s);
713 }
714
715 fifo8_create(&s->incoming_fifo, sizeof(long));
716
717 register_savevm(DEVICE(dev), "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
718 dev);
719
720 /* IRQFD requires MSI */
721 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
722 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
723 error_report("ioeventfd/irqfd requires MSI");
724 exit(1);
725 }
726
727 /* check that role is reasonable */
728 if (s->role) {
729 if (strncmp(s->role, "peer", 5) == 0) {
730 s->role_val = IVSHMEM_PEER;
731 } else if (strncmp(s->role, "master", 7) == 0) {
732 s->role_val = IVSHMEM_MASTER;
733 } else {
734 error_report("'role' must be 'peer' or 'master'");
735 exit(1);
736 }
737 } else {
738 s->role_val = IVSHMEM_MASTER; /* default */
739 }
740
741 if (s->role_val == IVSHMEM_PEER) {
742 error_setg(&s->migration_blocker,
743 "Migration is disabled when using feature 'peer mode' in device 'ivshmem'");
744 migrate_add_blocker(s->migration_blocker);
745 }
746
747 pci_conf = dev->config;
748 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
749
750 pci_config_set_interrupt_pin(pci_conf, 1);
751
752 s->shm_fd = 0;
753
754 memory_region_init_io(&s->ivshmem_mmio, OBJECT(s), &ivshmem_mmio_ops, s,
755 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
756
757 /* region for registers*/
758 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
759 &s->ivshmem_mmio);
760
761 memory_region_init(&s->bar, OBJECT(s), "ivshmem-bar2-container", s->ivshmem_size);
762 s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
763 PCI_BASE_ADDRESS_MEM_PREFETCH;
764 if (s->ivshmem_64bit) {
765 s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
766 }
767
768 if ((s->server_chr != NULL) &&
769 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
770 /* if we get a UNIX socket as the parameter we will talk
771 * to the ivshmem server to receive the memory region */
772
773 if (s->shmobj != NULL) {
774 error_report("WARNING: do not specify both 'chardev' "
775 "and 'shm' with ivshmem");
776 }
777
778 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
779 s->server_chr->filename);
780
781 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
782 ivshmem_setup_msi(s);
783 }
784
785 /* we allocate enough space for 16 guests and grow as needed */
786 s->nb_peers = 16;
787 s->vm_id = -1;
788
789 /* allocate/initialize space for interrupt handling */
790 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
791
792 pci_register_bar(dev, 2, s->ivshmem_attr, &s->bar);
793
794 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
795
796 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
797 ivshmem_event, s);
798 } else {
799 /* just map the file immediately, we're not using a server */
800 int fd;
801
802 if (s->shmobj == NULL) {
803 error_report("Must specify 'chardev' or 'shm' to ivshmem");
804 exit(1);
805 }
806
807 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
808
809 /* try opening with O_EXCL and if it succeeds zero the memory
810 * by truncating to 0 */
811 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
812 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
813 /* truncate file to length PCI device's memory */
814 if (ftruncate(fd, s->ivshmem_size) != 0) {
815 error_report("could not truncate shared file");
816 }
817
818 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
819 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
820 error_report("could not open shared file");
821 exit(1);
822
823 }
824
825 if (check_shm_size(s, fd) == -1) {
826 exit(1);
827 }
828
829 create_shared_memory_BAR(s, fd);
830
831 }
832
833 dev->config_write = ivshmem_write_config;
834
835 return 0;
836 }
837
838 static void pci_ivshmem_uninit(PCIDevice *dev)
839 {
840 IVShmemState *s = IVSHMEM(dev);
841
842 if (s->migration_blocker) {
843 migrate_del_blocker(s->migration_blocker);
844 error_free(s->migration_blocker);
845 }
846
847 memory_region_del_subregion(&s->bar, &s->ivshmem);
848 vmstate_unregister_ram(&s->ivshmem, DEVICE(dev));
849 unregister_savevm(DEVICE(dev), "ivshmem", s);
850 fifo8_destroy(&s->incoming_fifo);
851 }
852
853 static Property ivshmem_properties[] = {
854 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
855 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
856 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
857 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
858 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
859 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
860 DEFINE_PROP_STRING("role", IVShmemState, role),
861 DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
862 DEFINE_PROP_END_OF_LIST(),
863 };
864
865 static void ivshmem_class_init(ObjectClass *klass, void *data)
866 {
867 DeviceClass *dc = DEVICE_CLASS(klass);
868 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
869
870 k->init = pci_ivshmem_init;
871 k->exit = pci_ivshmem_uninit;
872 k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
873 k->device_id = PCI_DEVICE_ID_IVSHMEM;
874 k->class_id = PCI_CLASS_MEMORY_RAM;
875 dc->reset = ivshmem_reset;
876 dc->props = ivshmem_properties;
877 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
878 }
879
880 static const TypeInfo ivshmem_info = {
881 .name = TYPE_IVSHMEM,
882 .parent = TYPE_PCI_DEVICE,
883 .instance_size = sizeof(IVShmemState),
884 .class_init = ivshmem_class_init,
885 };
886
887 static void ivshmem_register_types(void)
888 {
889 type_register_static(&ivshmem_info);
890 }
891
892 type_init(ivshmem_register_types)