Statistics
| Branch: | Revision:

root / hw / ivshmem.c @ baaa86d9

History | View | Annotate | Download (21.5 kB)

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
#include "hw.h"
17
#include "pc.h"
18
#include "pci.h"
19
#include "msix.h"
20
#include "kvm.h"
21

    
22
#include <sys/mman.h>
23
#include <sys/types.h>
24

    
25
#define IVSHMEM_IOEVENTFD   0
26
#define IVSHMEM_MSI     1
27

    
28
#define IVSHMEM_PEER    0
29
#define IVSHMEM_MASTER  1
30

    
31
#define IVSHMEM_REG_BAR_SIZE 0x100
32

    
33
//#define DEBUG_IVSHMEM
34
#ifdef DEBUG_IVSHMEM
35
#define IVSHMEM_DPRINTF(fmt, ...)        \
36
    do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
37
#else
38
#define IVSHMEM_DPRINTF(fmt, ...)
39
#endif
40

    
41
typedef struct Peer {
42
    int nb_eventfds;
43
    int *eventfds;
44
} Peer;
45

    
46
typedef struct EventfdEntry {
47
    PCIDevice *pdev;
48
    int vector;
49
} EventfdEntry;
50

    
51
typedef struct IVShmemState {
52
    PCIDevice dev;
53
    uint32_t intrmask;
54
    uint32_t intrstatus;
55
    uint32_t doorbell;
56

    
57
    CharDriverState **eventfd_chr;
58
    CharDriverState *server_chr;
59
    MemoryRegion ivshmem_mmio;
60

    
61
    pcibus_t mmio_addr;
62
    /* We might need to register the BAR before we actually have the memory.
63
     * So prepare a container MemoryRegion for the BAR immediately and
64
     * add a subregion when we have the memory.
65
     */
66
    MemoryRegion bar;
67
    MemoryRegion ivshmem;
68
    MemoryRegion msix_bar;
69
    uint64_t ivshmem_size; /* size of shared memory region */
70
    int shm_fd; /* shared memory file descriptor */
71

    
72
    Peer *peers;
73
    int nb_peers; /* how many guests we have space for */
74
    int max_peer; /* maximum numbered peer */
75

    
76
    int vm_id;
77
    uint32_t vectors;
78
    uint32_t features;
79
    EventfdEntry *eventfd_table;
80

    
81
    char * shmobj;
82
    char * sizearg;
83
    char * role;
84
    int role_val;   /* scalar to avoid multiple string comparisons */
85
} IVShmemState;
86

    
87
/* registers for the Inter-VM shared memory device */
88
enum ivshmem_registers {
89
    INTRMASK = 0,
90
    INTRSTATUS = 4,
91
    IVPOSITION = 8,
92
    DOORBELL = 12,
93
};
94

    
95
static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
96
                                                    unsigned int feature) {
97
    return (ivs->features & (1 << feature));
98
}
99

    
100
static inline bool is_power_of_two(uint64_t x) {
101
    return (x & (x - 1)) == 0;
102
}
103

    
104
/* accessing registers - based on rtl8139 */
105
static void ivshmem_update_irq(IVShmemState *s, int val)
106
{
107
    int isr;
108
    isr = (s->intrstatus & s->intrmask) & 0xffffffff;
109

    
110
    /* don't print ISR resets */
111
    if (isr) {
112
        IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
113
           isr ? 1 : 0, s->intrstatus, s->intrmask);
114
    }
115

    
116
    qemu_set_irq(s->dev.irq[0], (isr != 0));
117
}
118

    
119
static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
120
{
121
    IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
122

    
123
    s->intrmask = val;
124

    
125
    ivshmem_update_irq(s, val);
126
}
127

    
128
static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
129
{
130
    uint32_t ret = s->intrmask;
131

    
132
    IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
133

    
134
    return ret;
135
}
136

    
137
static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
138
{
139
    IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
140

    
141
    s->intrstatus = val;
142

    
143
    ivshmem_update_irq(s, val);
144
    return;
145
}
146

    
147
static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
148
{
149
    uint32_t ret = s->intrstatus;
150

    
151
    /* reading ISR clears all interrupts */
152
    s->intrstatus = 0;
153

    
154
    ivshmem_update_irq(s, 0);
155

    
156
    return ret;
157
}
158

    
159
static void ivshmem_io_write(void *opaque, target_phys_addr_t addr,
160
                             uint64_t val, unsigned size)
161
{
162
    IVShmemState *s = opaque;
163

    
164
    uint64_t write_one = 1;
165
    uint16_t dest = val >> 16;
166
    uint16_t vector = val & 0xff;
167

    
168
    addr &= 0xfc;
169

    
170
    IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
171
    switch (addr)
172
    {
173
        case INTRMASK:
174
            ivshmem_IntrMask_write(s, val);
175
            break;
176

    
177
        case INTRSTATUS:
178
            ivshmem_IntrStatus_write(s, val);
179
            break;
180

    
181
        case DOORBELL:
182
            /* check that dest VM ID is reasonable */
183
            if (dest > s->max_peer) {
184
                IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
185
                break;
186
            }
187

    
188
            /* check doorbell range */
189
            if (vector < s->peers[dest].nb_eventfds) {
190
                IVSHMEM_DPRINTF("Writing %" PRId64 " to VM %d on vector %d\n",
191
                                                    write_one, dest, vector);
192
                if (write(s->peers[dest].eventfds[vector],
193
                                                    &(write_one), 8) != 8) {
194
                    IVSHMEM_DPRINTF("error writing to eventfd\n");
195
                }
196
            }
197
            break;
198
        default:
199
            IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
200
    }
201
}
202

    
203
static uint64_t ivshmem_io_read(void *opaque, target_phys_addr_t addr,
204
                                unsigned size)
205
{
206

    
207
    IVShmemState *s = opaque;
208
    uint32_t ret;
209

    
210
    switch (addr)
211
    {
212
        case INTRMASK:
213
            ret = ivshmem_IntrMask_read(s);
214
            break;
215

    
216
        case INTRSTATUS:
217
            ret = ivshmem_IntrStatus_read(s);
218
            break;
219

    
220
        case IVPOSITION:
221
            /* return my VM ID if the memory is mapped */
222
            if (s->shm_fd > 0) {
223
                ret = s->vm_id;
224
            } else {
225
                ret = -1;
226
            }
227
            break;
228

    
229
        default:
230
            IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
231
            ret = 0;
232
    }
233

    
234
    return ret;
235
}
236

    
237
static const MemoryRegionOps ivshmem_mmio_ops = {
238
    .read = ivshmem_io_read,
239
    .write = ivshmem_io_write,
240
    .endianness = DEVICE_NATIVE_ENDIAN,
241
    .impl = {
242
        .min_access_size = 4,
243
        .max_access_size = 4,
244
    },
245
};
246

    
247
static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
248
{
249
    IVShmemState *s = opaque;
250

    
251
    ivshmem_IntrStatus_write(s, *buf);
252

    
253
    IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
254
}
255

    
256
static int ivshmem_can_receive(void * opaque)
257
{
258
    return 8;
259
}
260

    
261
static void ivshmem_event(void *opaque, int event)
262
{
263
    IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
264
}
265

    
266
static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
267

    
268
    EventfdEntry *entry = opaque;
269
    PCIDevice *pdev = entry->pdev;
270

    
271
    IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
272
    msix_notify(pdev, entry->vector);
273
}
274

    
275
static CharDriverState* create_eventfd_chr_device(void * opaque, int eventfd,
276
                                                                    int vector)
277
{
278
    /* create a event character device based on the passed eventfd */
279
    IVShmemState *s = opaque;
280
    CharDriverState * chr;
281

    
282
    chr = qemu_chr_open_eventfd(eventfd);
283

    
284
    if (chr == NULL) {
285
        fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
286
        exit(-1);
287
    }
288

    
289
    /* if MSI is supported we need multiple interrupts */
290
    if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
291
        s->eventfd_table[vector].pdev = &s->dev;
292
        s->eventfd_table[vector].vector = vector;
293

    
294
        qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
295
                      ivshmem_event, &s->eventfd_table[vector]);
296
    } else {
297
        qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
298
                      ivshmem_event, s);
299
    }
300

    
301
    return chr;
302

    
303
}
304

    
305
static int check_shm_size(IVShmemState *s, int fd) {
306
    /* check that the guest isn't going to try and map more memory than the
307
     * the object has allocated return -1 to indicate error */
308

    
309
    struct stat buf;
310

    
311
    fstat(fd, &buf);
312

    
313
    if (s->ivshmem_size > buf.st_size) {
314
        fprintf(stderr,
315
                "IVSHMEM ERROR: Requested memory size greater"
316
                " than shared object size (%" PRIu64 " > %" PRIu64")\n",
317
                s->ivshmem_size, (uint64_t)buf.st_size);
318
        return -1;
319
    } else {
320
        return 0;
321
    }
322
}
323

    
324
/* create the shared memory BAR when we are not using the server, so we can
325
 * create the BAR and map the memory immediately */
326
static void create_shared_memory_BAR(IVShmemState *s, int fd) {
327

    
328
    void * ptr;
329

    
330
    s->shm_fd = fd;
331

    
332
    ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
333

    
334
    memory_region_init_ram_ptr(&s->ivshmem, &s->dev.qdev, "ivshmem.bar2",
335
                               s->ivshmem_size, ptr);
336
    memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
337

    
338
    /* region for shared memory */
339
    pci_register_bar(&s->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
340
}
341

    
342
static void close_guest_eventfds(IVShmemState *s, int posn)
343
{
344
    int i, guest_curr_max;
345

    
346
    guest_curr_max = s->peers[posn].nb_eventfds;
347

    
348
    for (i = 0; i < guest_curr_max; i++) {
349
        kvm_set_ioeventfd_mmio_long(s->peers[posn].eventfds[i],
350
                    s->mmio_addr + DOORBELL, (posn << 16) | i, 0);
351
        close(s->peers[posn].eventfds[i]);
352
    }
353

    
354
    g_free(s->peers[posn].eventfds);
355
    s->peers[posn].nb_eventfds = 0;
356
}
357

    
358
static void setup_ioeventfds(IVShmemState *s) {
359

    
360
    int i, j;
361

    
362
    for (i = 0; i <= s->max_peer; i++) {
363
        for (j = 0; j < s->peers[i].nb_eventfds; j++) {
364
            memory_region_add_eventfd(&s->ivshmem_mmio,
365
                                      DOORBELL,
366
                                      4,
367
                                      true,
368
                                      (i << 16) | j,
369
                                      s->peers[i].eventfds[j]);
370
        }
371
    }
372
}
373

    
374
/* this function increase the dynamic storage need to store data about other
375
 * guests */
376
static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
377

    
378
    int j, old_nb_alloc;
379

    
380
    old_nb_alloc = s->nb_peers;
381

    
382
    while (new_min_size >= s->nb_peers)
383
        s->nb_peers = s->nb_peers * 2;
384

    
385
    IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
386
    s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
387

    
388
    /* zero out new pointers */
389
    for (j = old_nb_alloc; j < s->nb_peers; j++) {
390
        s->peers[j].eventfds = NULL;
391
        s->peers[j].nb_eventfds = 0;
392
    }
393
}
394

    
395
static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
396
{
397
    IVShmemState *s = opaque;
398
    int incoming_fd, tmp_fd;
399
    int guest_max_eventfd;
400
    long incoming_posn;
401

    
402
    memcpy(&incoming_posn, buf, sizeof(long));
403
    /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
404
    tmp_fd = qemu_chr_get_msgfd(s->server_chr);
405
    IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
406

    
407
    /* make sure we have enough space for this guest */
408
    if (incoming_posn >= s->nb_peers) {
409
        increase_dynamic_storage(s, incoming_posn);
410
    }
411

    
412
    if (tmp_fd == -1) {
413
        /* if posn is positive and unseen before then this is our posn*/
414
        if ((incoming_posn >= 0) &&
415
                            (s->peers[incoming_posn].eventfds == NULL)) {
416
            /* receive our posn */
417
            s->vm_id = incoming_posn;
418
            return;
419
        } else {
420
            /* otherwise an fd == -1 means an existing guest has gone away */
421
            IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
422
            close_guest_eventfds(s, incoming_posn);
423
            return;
424
        }
425
    }
426

    
427
    /* because of the implementation of get_msgfd, we need a dup */
428
    incoming_fd = dup(tmp_fd);
429

    
430
    if (incoming_fd == -1) {
431
        fprintf(stderr, "could not allocate file descriptor %s\n",
432
                                                            strerror(errno));
433
        return;
434
    }
435

    
436
    /* if the position is -1, then it's shared memory region fd */
437
    if (incoming_posn == -1) {
438

    
439
        void * map_ptr;
440

    
441
        s->max_peer = 0;
442

    
443
        if (check_shm_size(s, incoming_fd) == -1) {
444
            exit(-1);
445
        }
446

    
447
        /* mmap the region and map into the BAR2 */
448
        map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
449
                                                            incoming_fd, 0);
450
        memory_region_init_ram_ptr(&s->ivshmem, &s->dev.qdev,
451
                                   "ivshmem.bar2", s->ivshmem_size, map_ptr);
452

    
453
        IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
454
                         s->ivshmem_offset, s->ivshmem_size);
455

    
456
        memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
457

    
458
        /* only store the fd if it is successfully mapped */
459
        s->shm_fd = incoming_fd;
460

    
461
        return;
462
    }
463

    
464
    /* each guest has an array of eventfds, and we keep track of how many
465
     * guests for each VM */
466
    guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
467

    
468
    if (guest_max_eventfd == 0) {
469
        /* one eventfd per MSI vector */
470
        s->peers[incoming_posn].eventfds = (int *) g_malloc(s->vectors *
471
                                                                sizeof(int));
472
    }
473

    
474
    /* this is an eventfd for a particular guest VM */
475
    IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
476
                                            guest_max_eventfd, incoming_fd);
477
    s->peers[incoming_posn].eventfds[guest_max_eventfd] = incoming_fd;
478

    
479
    /* increment count for particular guest */
480
    s->peers[incoming_posn].nb_eventfds++;
481

    
482
    /* keep track of the maximum VM ID */
483
    if (incoming_posn > s->max_peer) {
484
        s->max_peer = incoming_posn;
485
    }
486

    
487
    if (incoming_posn == s->vm_id) {
488
        s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
489
                   s->peers[s->vm_id].eventfds[guest_max_eventfd],
490
                   guest_max_eventfd);
491
    }
492

    
493
    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
494
        if (kvm_set_ioeventfd_mmio_long(incoming_fd, s->mmio_addr + DOORBELL,
495
                        (incoming_posn << 16) | guest_max_eventfd, 1) < 0) {
496
            fprintf(stderr, "ivshmem: ioeventfd not available\n");
497
        }
498
    }
499

    
500
    return;
501
}
502

    
503
static void ivshmem_reset(DeviceState *d)
504
{
505
    IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
506

    
507
    s->intrstatus = 0;
508
    return;
509
}
510

    
511
static uint64_t ivshmem_get_size(IVShmemState * s) {
512

    
513
    uint64_t value;
514
    char *ptr;
515

    
516
    value = strtoull(s->sizearg, &ptr, 10);
517
    switch (*ptr) {
518
        case 0: case 'M': case 'm':
519
            value <<= 20;
520
            break;
521
        case 'G': case 'g':
522
            value <<= 30;
523
            break;
524
        default:
525
            fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
526
            exit(1);
527
    }
528

    
529
    /* BARs must be a power of 2 */
530
    if (!is_power_of_two(value)) {
531
        fprintf(stderr, "ivshmem: size must be power of 2\n");
532
        exit(1);
533
    }
534

    
535
    return value;
536
}
537

    
538
static void ivshmem_setup_msi(IVShmemState * s) {
539

    
540
    int i;
541

    
542
    /* allocate the MSI-X vectors */
543

    
544
    memory_region_init(&s->msix_bar, "ivshmem-msix", 4096);
545
    if (!msix_init(&s->dev, s->vectors, &s->msix_bar, 1, 0)) {
546
        pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
547
                         &s->msix_bar);
548
        IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
549
    } else {
550
        IVSHMEM_DPRINTF("msix initialization failed\n");
551
        exit(1);
552
    }
553

    
554
    /* 'activate' the vectors */
555
    for (i = 0; i < s->vectors; i++) {
556
        msix_vector_use(&s->dev, i);
557
    }
558

    
559
    /* allocate Qemu char devices for receiving interrupts */
560
    s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
561
}
562

    
563
static void ivshmem_save(QEMUFile* f, void *opaque)
564
{
565
    IVShmemState *proxy = opaque;
566

    
567
    IVSHMEM_DPRINTF("ivshmem_save\n");
568
    pci_device_save(&proxy->dev, f);
569

    
570
    if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
571
        msix_save(&proxy->dev, f);
572
    } else {
573
        qemu_put_be32(f, proxy->intrstatus);
574
        qemu_put_be32(f, proxy->intrmask);
575
    }
576

    
577
}
578

    
579
static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
580
{
581
    IVSHMEM_DPRINTF("ivshmem_load\n");
582

    
583
    IVShmemState *proxy = opaque;
584
    int ret, i;
585

    
586
    if (version_id > 0) {
587
        return -EINVAL;
588
    }
589

    
590
    if (proxy->role_val == IVSHMEM_PEER) {
591
        fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
592
        return -EINVAL;
593
    }
594

    
595
    ret = pci_device_load(&proxy->dev, f);
596
    if (ret) {
597
        return ret;
598
    }
599

    
600
    if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
601
        msix_load(&proxy->dev, f);
602
        for (i = 0; i < proxy->vectors; i++) {
603
            msix_vector_use(&proxy->dev, i);
604
        }
605
    } else {
606
        proxy->intrstatus = qemu_get_be32(f);
607
        proxy->intrmask = qemu_get_be32(f);
608
    }
609

    
610
    return 0;
611
}
612

    
613
static int pci_ivshmem_init(PCIDevice *dev)
614
{
615
    IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
616
    uint8_t *pci_conf;
617

    
618
    if (s->sizearg == NULL)
619
        s->ivshmem_size = 4 << 20; /* 4 MB default */
620
    else {
621
        s->ivshmem_size = ivshmem_get_size(s);
622
    }
623

    
624
    register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
625
                                                                        dev);
626

    
627
    /* IRQFD requires MSI */
628
    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
629
        !ivshmem_has_feature(s, IVSHMEM_MSI)) {
630
        fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
631
        exit(1);
632
    }
633

    
634
    /* check that role is reasonable */
635
    if (s->role) {
636
        if (strncmp(s->role, "peer", 5) == 0) {
637
            s->role_val = IVSHMEM_PEER;
638
        } else if (strncmp(s->role, "master", 7) == 0) {
639
            s->role_val = IVSHMEM_MASTER;
640
        } else {
641
            fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
642
            exit(1);
643
        }
644
    } else {
645
        s->role_val = IVSHMEM_MASTER; /* default */
646
    }
647

    
648
    if (s->role_val == IVSHMEM_PEER) {
649
        register_device_unmigratable(&s->dev.qdev, "ivshmem", s);
650
    }
651

    
652
    pci_conf = s->dev.config;
653
    pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
654

    
655
    pci_config_set_interrupt_pin(pci_conf, 1);
656

    
657
    s->shm_fd = 0;
658

    
659
    memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
660
                          "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
661

    
662
    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
663
        setup_ioeventfds(s);
664
    }
665

    
666
    /* region for registers*/
667
    pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
668
                     &s->ivshmem_mmio);
669

    
670
    memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
671

    
672
    if ((s->server_chr != NULL) &&
673
                        (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
674
        /* if we get a UNIX socket as the parameter we will talk
675
         * to the ivshmem server to receive the memory region */
676

    
677
        if (s->shmobj != NULL) {
678
            fprintf(stderr, "WARNING: do not specify both 'chardev' "
679
                                                "and 'shm' with ivshmem\n");
680
        }
681

    
682
        IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
683
                                                    s->server_chr->filename);
684

    
685
        if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
686
            ivshmem_setup_msi(s);
687
        }
688

    
689
        /* we allocate enough space for 16 guests and grow as needed */
690
        s->nb_peers = 16;
691
        s->vm_id = -1;
692

    
693
        /* allocate/initialize space for interrupt handling */
694
        s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
695

    
696
        pci_register_bar(&s->dev, 2,
697
                         PCI_BASE_ADDRESS_SPACE_MEMORY, &s->ivshmem);
698

    
699
        s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
700

    
701
        qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
702
                     ivshmem_event, s);
703
    } else {
704
        /* just map the file immediately, we're not using a server */
705
        int fd;
706

    
707
        if (s->shmobj == NULL) {
708
            fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
709
        }
710

    
711
        IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
712

    
713
        /* try opening with O_EXCL and if it succeeds zero the memory
714
         * by truncating to 0 */
715
        if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
716
                        S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
717
           /* truncate file to length PCI device's memory */
718
            if (ftruncate(fd, s->ivshmem_size) != 0) {
719
                fprintf(stderr, "ivshmem: could not truncate shared file\n");
720
            }
721

    
722
        } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
723
                        S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
724
            fprintf(stderr, "ivshmem: could not open shared file\n");
725
            exit(-1);
726

    
727
        }
728

    
729
        if (check_shm_size(s, fd) == -1) {
730
            exit(-1);
731
        }
732

    
733
        create_shared_memory_BAR(s, fd);
734

    
735
    }
736

    
737
    return 0;
738
}
739

    
740
static int pci_ivshmem_uninit(PCIDevice *dev)
741
{
742
    IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
743

    
744
    memory_region_destroy(&s->ivshmem_mmio);
745
    memory_region_del_subregion(&s->bar, &s->ivshmem);
746
    memory_region_destroy(&s->ivshmem);
747
    memory_region_destroy(&s->bar);
748
    unregister_savevm(&dev->qdev, "ivshmem", s);
749

    
750
    return 0;
751
}
752

    
753
static PCIDeviceInfo ivshmem_info = {
754
    .qdev.name  = "ivshmem",
755
    .qdev.size  = sizeof(IVShmemState),
756
    .qdev.reset = ivshmem_reset,
757
    .init       = pci_ivshmem_init,
758
    .exit       = pci_ivshmem_uninit,
759
    .vendor_id  = PCI_VENDOR_ID_REDHAT_QUMRANET,
760
    .device_id  = 0x1110,
761
    .class_id   = PCI_CLASS_MEMORY_RAM,
762
    .qdev.props = (Property[]) {
763
        DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
764
        DEFINE_PROP_STRING("size", IVShmemState, sizearg),
765
        DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
766
        DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
767
        DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
768
        DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
769
        DEFINE_PROP_STRING("role", IVShmemState, role),
770
        DEFINE_PROP_END_OF_LIST(),
771
    }
772
};
773

    
774
static void ivshmem_register_devices(void)
775
{
776
    pci_qdev_register(&ivshmem_info);
777
}
778

    
779
device_init(ivshmem_register_devices)