]> git.proxmox.com Git - mirror_qemu.git/blob - hw/ivshmem.c
59f1aa4265ff354d29ec8db3a002d68284ed16d8
[mirror_qemu.git] / hw / 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.h"
20 #include "pc.h"
21 #include "pci.h"
22 #include "msix.h"
23 #include "kvm.h"
24 #include "migration.h"
25 #include "qerror.h"
26 #include "event_notifier.h"
27
28 #include <sys/mman.h>
29 #include <sys/types.h>
30
31 #define IVSHMEM_IOEVENTFD 0
32 #define IVSHMEM_MSI 1
33
34 #define IVSHMEM_PEER 0
35 #define IVSHMEM_MASTER 1
36
37 #define IVSHMEM_REG_BAR_SIZE 0x100
38
39 //#define DEBUG_IVSHMEM
40 #ifdef DEBUG_IVSHMEM
41 #define IVSHMEM_DPRINTF(fmt, ...) \
42 do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
43 #else
44 #define IVSHMEM_DPRINTF(fmt, ...)
45 #endif
46
47 typedef struct Peer {
48 int nb_eventfds;
49 EventNotifier *eventfds;
50 } Peer;
51
52 typedef struct EventfdEntry {
53 PCIDevice *pdev;
54 int vector;
55 } EventfdEntry;
56
57 typedef struct IVShmemState {
58 PCIDevice dev;
59 uint32_t intrmask;
60 uint32_t intrstatus;
61 uint32_t doorbell;
62
63 CharDriverState **eventfd_chr;
64 CharDriverState *server_chr;
65 MemoryRegion ivshmem_mmio;
66
67 /* We might need to register the BAR before we actually have the memory.
68 * So prepare a container MemoryRegion for the BAR immediately and
69 * add a subregion when we have the memory.
70 */
71 MemoryRegion bar;
72 MemoryRegion ivshmem;
73 uint64_t ivshmem_size; /* size of shared memory region */
74 uint32_t ivshmem_attr;
75 uint32_t ivshmem_64bit;
76 int shm_fd; /* shared memory file descriptor */
77
78 Peer *peers;
79 int nb_peers; /* how many guests we have space for */
80 int max_peer; /* maximum numbered peer */
81
82 int vm_id;
83 uint32_t vectors;
84 uint32_t features;
85 EventfdEntry *eventfd_table;
86
87 Error *migration_blocker;
88
89 char * shmobj;
90 char * sizearg;
91 char * role;
92 int role_val; /* scalar to avoid multiple string comparisons */
93 } IVShmemState;
94
95 /* registers for the Inter-VM shared memory device */
96 enum ivshmem_registers {
97 INTRMASK = 0,
98 INTRSTATUS = 4,
99 IVPOSITION = 8,
100 DOORBELL = 12,
101 };
102
103 static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
104 unsigned int feature) {
105 return (ivs->features & (1 << feature));
106 }
107
108 static inline bool is_power_of_two(uint64_t x) {
109 return (x & (x - 1)) == 0;
110 }
111
112 /* accessing registers - based on rtl8139 */
113 static void ivshmem_update_irq(IVShmemState *s, int val)
114 {
115 int isr;
116 isr = (s->intrstatus & s->intrmask) & 0xffffffff;
117
118 /* don't print ISR resets */
119 if (isr) {
120 IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
121 isr ? 1 : 0, s->intrstatus, s->intrmask);
122 }
123
124 qemu_set_irq(s->dev.irq[0], (isr != 0));
125 }
126
127 static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
128 {
129 IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
130
131 s->intrmask = val;
132
133 ivshmem_update_irq(s, val);
134 }
135
136 static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
137 {
138 uint32_t ret = s->intrmask;
139
140 IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
141
142 return ret;
143 }
144
145 static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
146 {
147 IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
148
149 s->intrstatus = val;
150
151 ivshmem_update_irq(s, val);
152 return;
153 }
154
155 static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
156 {
157 uint32_t ret = s->intrstatus;
158
159 /* reading ISR clears all interrupts */
160 s->intrstatus = 0;
161
162 ivshmem_update_irq(s, 0);
163
164 return ret;
165 }
166
167 static void ivshmem_io_write(void *opaque, target_phys_addr_t addr,
168 uint64_t val, unsigned size)
169 {
170 IVShmemState *s = opaque;
171
172 uint16_t dest = val >> 16;
173 uint16_t vector = val & 0xff;
174
175 addr &= 0xfc;
176
177 IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
178 switch (addr)
179 {
180 case INTRMASK:
181 ivshmem_IntrMask_write(s, val);
182 break;
183
184 case INTRSTATUS:
185 ivshmem_IntrStatus_write(s, val);
186 break;
187
188 case DOORBELL:
189 /* check that dest VM ID is reasonable */
190 if (dest > s->max_peer) {
191 IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
192 break;
193 }
194
195 /* check doorbell range */
196 if (vector < s->peers[dest].nb_eventfds) {
197 IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
198 event_notifier_set(&s->peers[dest].eventfds[vector]);
199 }
200 break;
201 default:
202 IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
203 }
204 }
205
206 static uint64_t ivshmem_io_read(void *opaque, target_phys_addr_t addr,
207 unsigned size)
208 {
209
210 IVShmemState *s = opaque;
211 uint32_t ret;
212
213 switch (addr)
214 {
215 case INTRMASK:
216 ret = ivshmem_IntrMask_read(s);
217 break;
218
219 case INTRSTATUS:
220 ret = ivshmem_IntrStatus_read(s);
221 break;
222
223 case IVPOSITION:
224 /* return my VM ID if the memory is mapped */
225 if (s->shm_fd > 0) {
226 ret = s->vm_id;
227 } else {
228 ret = -1;
229 }
230 break;
231
232 default:
233 IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
234 ret = 0;
235 }
236
237 return ret;
238 }
239
240 static const MemoryRegionOps ivshmem_mmio_ops = {
241 .read = ivshmem_io_read,
242 .write = ivshmem_io_write,
243 .endianness = DEVICE_NATIVE_ENDIAN,
244 .impl = {
245 .min_access_size = 4,
246 .max_access_size = 4,
247 },
248 };
249
250 static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
251 {
252 IVShmemState *s = opaque;
253
254 ivshmem_IntrStatus_write(s, *buf);
255
256 IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
257 }
258
259 static int ivshmem_can_receive(void * opaque)
260 {
261 return 8;
262 }
263
264 static void ivshmem_event(void *opaque, int event)
265 {
266 IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
267 }
268
269 static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
270
271 EventfdEntry *entry = opaque;
272 PCIDevice *pdev = entry->pdev;
273
274 IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
275 msix_notify(pdev, entry->vector);
276 }
277
278 static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
279 int vector)
280 {
281 /* create a event character device based on the passed eventfd */
282 IVShmemState *s = opaque;
283 CharDriverState * chr;
284 int eventfd = event_notifier_get_fd(n);
285
286 chr = qemu_chr_open_eventfd(eventfd);
287
288 if (chr == NULL) {
289 fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
290 exit(-1);
291 }
292
293 /* if MSI is supported we need multiple interrupts */
294 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
295 s->eventfd_table[vector].pdev = &s->dev;
296 s->eventfd_table[vector].vector = vector;
297
298 qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
299 ivshmem_event, &s->eventfd_table[vector]);
300 } else {
301 qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
302 ivshmem_event, s);
303 }
304
305 return chr;
306
307 }
308
309 static int check_shm_size(IVShmemState *s, int fd) {
310 /* check that the guest isn't going to try and map more memory than the
311 * the object has allocated return -1 to indicate error */
312
313 struct stat buf;
314
315 fstat(fd, &buf);
316
317 if (s->ivshmem_size > buf.st_size) {
318 fprintf(stderr,
319 "IVSHMEM ERROR: Requested memory size greater"
320 " than shared object size (%" PRIu64 " > %" PRIu64")\n",
321 s->ivshmem_size, (uint64_t)buf.st_size);
322 return -1;
323 } else {
324 return 0;
325 }
326 }
327
328 /* create the shared memory BAR when we are not using the server, so we can
329 * create the BAR and map the memory immediately */
330 static void create_shared_memory_BAR(IVShmemState *s, int fd) {
331
332 void * ptr;
333
334 s->shm_fd = fd;
335
336 ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
337
338 memory_region_init_ram_ptr(&s->ivshmem, "ivshmem.bar2",
339 s->ivshmem_size, ptr);
340 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
341 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
342
343 /* region for shared memory */
344 pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
345 }
346
347 static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
348 {
349 memory_region_add_eventfd(&s->ivshmem_mmio,
350 DOORBELL,
351 4,
352 true,
353 (posn << 16) | i,
354 &s->peers[posn].eventfds[i]);
355 }
356
357 static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
358 {
359 memory_region_del_eventfd(&s->ivshmem_mmio,
360 DOORBELL,
361 4,
362 true,
363 (posn << 16) | i,
364 &s->peers[posn].eventfds[i]);
365 }
366
367 static void close_guest_eventfds(IVShmemState *s, int posn)
368 {
369 int i, guest_curr_max;
370
371 if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
372 return;
373 }
374
375 guest_curr_max = s->peers[posn].nb_eventfds;
376
377 memory_region_transaction_begin();
378 for (i = 0; i < guest_curr_max; i++) {
379 ivshmem_del_eventfd(s, posn, i);
380 }
381 memory_region_transaction_commit();
382 for (i = 0; i < guest_curr_max; i++) {
383 event_notifier_cleanup(&s->peers[posn].eventfds[i]);
384 }
385
386 g_free(s->peers[posn].eventfds);
387 s->peers[posn].nb_eventfds = 0;
388 }
389
390 /* this function increase the dynamic storage need to store data about other
391 * guests */
392 static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
393
394 int j, old_nb_alloc;
395
396 old_nb_alloc = s->nb_peers;
397
398 while (new_min_size >= s->nb_peers)
399 s->nb_peers = s->nb_peers * 2;
400
401 IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
402 s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
403
404 /* zero out new pointers */
405 for (j = old_nb_alloc; j < s->nb_peers; j++) {
406 s->peers[j].eventfds = NULL;
407 s->peers[j].nb_eventfds = 0;
408 }
409 }
410
411 static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
412 {
413 IVShmemState *s = opaque;
414 int incoming_fd, tmp_fd;
415 int guest_max_eventfd;
416 long incoming_posn;
417
418 memcpy(&incoming_posn, buf, sizeof(long));
419 /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
420 tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
421 IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
422
423 /* make sure we have enough space for this guest */
424 if (incoming_posn >= s->nb_peers) {
425 increase_dynamic_storage(s, incoming_posn);
426 }
427
428 if (tmp_fd == -1) {
429 /* if posn is positive and unseen before then this is our posn*/
430 if ((incoming_posn >= 0) &&
431 (s->peers[incoming_posn].eventfds == NULL)) {
432 /* receive our posn */
433 s->vm_id = incoming_posn;
434 return;
435 } else {
436 /* otherwise an fd == -1 means an existing guest has gone away */
437 IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
438 close_guest_eventfds(s, incoming_posn);
439 return;
440 }
441 }
442
443 /* because of the implementation of get_msgfd, we need a dup */
444 incoming_fd = dup(tmp_fd);
445
446 if (incoming_fd == -1) {
447 fprintf(stderr, "could not allocate file descriptor %s\n",
448 strerror(errno));
449 return;
450 }
451
452 /* if the position is -1, then it's shared memory region fd */
453 if (incoming_posn == -1) {
454
455 void * map_ptr;
456
457 s->max_peer = 0;
458
459 if (check_shm_size(s, incoming_fd) == -1) {
460 exit(-1);
461 }
462
463 /* mmap the region and map into the BAR2 */
464 map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
465 incoming_fd, 0);
466 memory_region_init_ram_ptr(&s->ivshmem,
467 "ivshmem.bar2", s->ivshmem_size, map_ptr);
468 vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
469
470 IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
471 s->ivshmem_offset, s->ivshmem_size);
472
473 memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
474
475 /* only store the fd if it is successfully mapped */
476 s->shm_fd = incoming_fd;
477
478 return;
479 }
480
481 /* each guest has an array of eventfds, and we keep track of how many
482 * guests for each VM */
483 guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
484
485 if (guest_max_eventfd == 0) {
486 /* one eventfd per MSI vector */
487 s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
488 }
489
490 /* this is an eventfd for a particular guest VM */
491 IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
492 guest_max_eventfd, incoming_fd);
493 event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
494 incoming_fd);
495
496 /* increment count for particular guest */
497 s->peers[incoming_posn].nb_eventfds++;
498
499 /* keep track of the maximum VM ID */
500 if (incoming_posn > s->max_peer) {
501 s->max_peer = incoming_posn;
502 }
503
504 if (incoming_posn == s->vm_id) {
505 s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
506 &s->peers[s->vm_id].eventfds[guest_max_eventfd],
507 guest_max_eventfd);
508 }
509
510 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
511 ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
512 }
513
514 return;
515 }
516
517 /* Select the MSI-X vectors used by device.
518 * ivshmem maps events to vectors statically, so
519 * we just enable all vectors on init and after reset. */
520 static void ivshmem_use_msix(IVShmemState * s)
521 {
522 int i;
523
524 if (!msix_present(&s->dev)) {
525 return;
526 }
527
528 for (i = 0; i < s->vectors; i++) {
529 msix_vector_use(&s->dev, i);
530 }
531 }
532
533 static void ivshmem_reset(DeviceState *d)
534 {
535 IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
536
537 s->intrstatus = 0;
538 ivshmem_use_msix(s);
539 return;
540 }
541
542 static uint64_t ivshmem_get_size(IVShmemState * s) {
543
544 uint64_t value;
545 char *ptr;
546
547 value = strtoull(s->sizearg, &ptr, 10);
548 switch (*ptr) {
549 case 0: case 'M': case 'm':
550 value <<= 20;
551 break;
552 case 'G': case 'g':
553 value <<= 30;
554 break;
555 default:
556 fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
557 exit(1);
558 }
559
560 /* BARs must be a power of 2 */
561 if (!is_power_of_two(value)) {
562 fprintf(stderr, "ivshmem: size must be power of 2\n");
563 exit(1);
564 }
565
566 return value;
567 }
568
569 static void ivshmem_setup_msi(IVShmemState * s)
570 {
571 if (msix_init_exclusive_bar(&s->dev, s->vectors, 1)) {
572 IVSHMEM_DPRINTF("msix initialization failed\n");
573 exit(1);
574 }
575
576 IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
577
578 /* allocate QEMU char devices for receiving interrupts */
579 s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
580
581 ivshmem_use_msix(s);
582 }
583
584 static void ivshmem_save(QEMUFile* f, void *opaque)
585 {
586 IVShmemState *proxy = opaque;
587
588 IVSHMEM_DPRINTF("ivshmem_save\n");
589 pci_device_save(&proxy->dev, f);
590
591 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
592 msix_save(&proxy->dev, f);
593 } else {
594 qemu_put_be32(f, proxy->intrstatus);
595 qemu_put_be32(f, proxy->intrmask);
596 }
597
598 }
599
600 static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
601 {
602 IVSHMEM_DPRINTF("ivshmem_load\n");
603
604 IVShmemState *proxy = opaque;
605 int ret;
606
607 if (version_id > 0) {
608 return -EINVAL;
609 }
610
611 if (proxy->role_val == IVSHMEM_PEER) {
612 fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
613 return -EINVAL;
614 }
615
616 ret = pci_device_load(&proxy->dev, f);
617 if (ret) {
618 return ret;
619 }
620
621 if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
622 msix_load(&proxy->dev, f);
623 ivshmem_use_msix(proxy);
624 } else {
625 proxy->intrstatus = qemu_get_be32(f);
626 proxy->intrmask = qemu_get_be32(f);
627 }
628
629 return 0;
630 }
631
632 static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
633 uint32_t val, int len)
634 {
635 pci_default_write_config(pci_dev, address, val, len);
636 msix_write_config(pci_dev, address, val, len);
637 }
638
639 static int pci_ivshmem_init(PCIDevice *dev)
640 {
641 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
642 uint8_t *pci_conf;
643
644 if (s->sizearg == NULL)
645 s->ivshmem_size = 4 << 20; /* 4 MB default */
646 else {
647 s->ivshmem_size = ivshmem_get_size(s);
648 }
649
650 register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
651 dev);
652
653 /* IRQFD requires MSI */
654 if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
655 !ivshmem_has_feature(s, IVSHMEM_MSI)) {
656 fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
657 exit(1);
658 }
659
660 /* check that role is reasonable */
661 if (s->role) {
662 if (strncmp(s->role, "peer", 5) == 0) {
663 s->role_val = IVSHMEM_PEER;
664 } else if (strncmp(s->role, "master", 7) == 0) {
665 s->role_val = IVSHMEM_MASTER;
666 } else {
667 fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
668 exit(1);
669 }
670 } else {
671 s->role_val = IVSHMEM_MASTER; /* default */
672 }
673
674 if (s->role_val == IVSHMEM_PEER) {
675 error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
676 "peer mode", "ivshmem");
677 migrate_add_blocker(s->migration_blocker);
678 }
679
680 pci_conf = s->dev.config;
681 pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
682
683 pci_config_set_interrupt_pin(pci_conf, 1);
684
685 s->shm_fd = 0;
686
687 memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
688 "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
689
690 /* region for registers*/
691 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
692 &s->ivshmem_mmio);
693
694 memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
695 s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
696 PCI_BASE_ADDRESS_MEM_PREFETCH;
697 if (s->ivshmem_64bit) {
698 s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
699 }
700
701 if ((s->server_chr != NULL) &&
702 (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
703 /* if we get a UNIX socket as the parameter we will talk
704 * to the ivshmem server to receive the memory region */
705
706 if (s->shmobj != NULL) {
707 fprintf(stderr, "WARNING: do not specify both 'chardev' "
708 "and 'shm' with ivshmem\n");
709 }
710
711 IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
712 s->server_chr->filename);
713
714 if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
715 ivshmem_setup_msi(s);
716 }
717
718 /* we allocate enough space for 16 guests and grow as needed */
719 s->nb_peers = 16;
720 s->vm_id = -1;
721
722 /* allocate/initialize space for interrupt handling */
723 s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
724
725 pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
726
727 s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
728
729 qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
730 ivshmem_event, s);
731 } else {
732 /* just map the file immediately, we're not using a server */
733 int fd;
734
735 if (s->shmobj == NULL) {
736 fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
737 }
738
739 IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
740
741 /* try opening with O_EXCL and if it succeeds zero the memory
742 * by truncating to 0 */
743 if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
744 S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
745 /* truncate file to length PCI device's memory */
746 if (ftruncate(fd, s->ivshmem_size) != 0) {
747 fprintf(stderr, "ivshmem: could not truncate shared file\n");
748 }
749
750 } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
751 S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
752 fprintf(stderr, "ivshmem: could not open shared file\n");
753 exit(-1);
754
755 }
756
757 if (check_shm_size(s, fd) == -1) {
758 exit(-1);
759 }
760
761 create_shared_memory_BAR(s, fd);
762
763 }
764
765 s->dev.config_write = ivshmem_write_config;
766
767 return 0;
768 }
769
770 static void pci_ivshmem_uninit(PCIDevice *dev)
771 {
772 IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
773
774 if (s->migration_blocker) {
775 migrate_del_blocker(s->migration_blocker);
776 error_free(s->migration_blocker);
777 }
778
779 memory_region_destroy(&s->ivshmem_mmio);
780 memory_region_del_subregion(&s->bar, &s->ivshmem);
781 vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
782 memory_region_destroy(&s->ivshmem);
783 memory_region_destroy(&s->bar);
784 unregister_savevm(&dev->qdev, "ivshmem", s);
785 }
786
787 static Property ivshmem_properties[] = {
788 DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
789 DEFINE_PROP_STRING("size", IVShmemState, sizearg),
790 DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
791 DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
792 DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
793 DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
794 DEFINE_PROP_STRING("role", IVShmemState, role),
795 DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
796 DEFINE_PROP_END_OF_LIST(),
797 };
798
799 static void ivshmem_class_init(ObjectClass *klass, void *data)
800 {
801 DeviceClass *dc = DEVICE_CLASS(klass);
802 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
803
804 k->init = pci_ivshmem_init;
805 k->exit = pci_ivshmem_uninit;
806 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
807 k->device_id = 0x1110;
808 k->class_id = PCI_CLASS_MEMORY_RAM;
809 dc->reset = ivshmem_reset;
810 dc->props = ivshmem_properties;
811 }
812
813 static TypeInfo ivshmem_info = {
814 .name = "ivshmem",
815 .parent = TYPE_PCI_DEVICE,
816 .instance_size = sizeof(IVShmemState),
817 .class_init = ivshmem_class_init,
818 };
819
820 static void ivshmem_register_types(void)
821 {
822 type_register_static(&ivshmem_info);
823 }
824
825 type_init(ivshmem_register_types)