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