Statistics
| Branch: | Revision:

root / hw / ivshmem.c @ 927d4878

History | View | Annotate | Download (22.6 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
 * 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/pci.h"
22
#include "pci/msix.h"
23
#include "sysemu/kvm.h"
24
#include "migration/migration.h"
25
#include "qapi/qmp/qerror.h"
26
#include "qemu/event_notifier.h"
27
#include "char/char.h"
28

    
29
#include <sys/mman.h>
30
#include <sys/types.h>
31

    
32
#define IVSHMEM_IOEVENTFD   0
33
#define IVSHMEM_MSI     1
34

    
35
#define IVSHMEM_PEER    0
36
#define IVSHMEM_MASTER  1
37

    
38
#define IVSHMEM_REG_BAR_SIZE 0x100
39

    
40
//#define DEBUG_IVSHMEM
41
#ifdef DEBUG_IVSHMEM
42
#define IVSHMEM_DPRINTF(fmt, ...)        \
43
    do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
44
#else
45
#define IVSHMEM_DPRINTF(fmt, ...)
46
#endif
47

    
48
typedef struct Peer {
49
    int nb_eventfds;
50
    EventNotifier *eventfds;
51
} Peer;
52

    
53
typedef struct EventfdEntry {
54
    PCIDevice *pdev;
55
    int vector;
56
} EventfdEntry;
57

    
58
typedef struct IVShmemState {
59
    PCIDevice dev;
60
    uint32_t intrmask;
61
    uint32_t intrstatus;
62
    uint32_t doorbell;
63

    
64
    CharDriverState **eventfd_chr;
65
    CharDriverState *server_chr;
66
    MemoryRegion ivshmem_mmio;
67

    
68
    /* We might need to register the BAR before we actually have the memory.
69
     * So prepare a container MemoryRegion for the BAR immediately and
70
     * add a subregion when we have the memory.
71
     */
72
    MemoryRegion bar;
73
    MemoryRegion ivshmem;
74
    uint64_t ivshmem_size; /* size of shared memory region */
75
    uint32_t ivshmem_attr;
76
    uint32_t ivshmem_64bit;
77
    int shm_fd; /* shared memory file descriptor */
78

    
79
    Peer *peers;
80
    int nb_peers; /* how many guests we have space for */
81
    int max_peer; /* maximum numbered peer */
82

    
83
    int vm_id;
84
    uint32_t vectors;
85
    uint32_t features;
86
    EventfdEntry *eventfd_table;
87

    
88
    Error *migration_blocker;
89

    
90
    char * shmobj;
91
    char * sizearg;
92
    char * role;
93
    int role_val;   /* scalar to avoid multiple string comparisons */
94
} IVShmemState;
95

    
96
/* registers for the Inter-VM shared memory device */
97
enum ivshmem_registers {
98
    INTRMASK = 0,
99
    INTRSTATUS = 4,
100
    IVPOSITION = 8,
101
    DOORBELL = 12,
102
};
103

    
104
static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
105
                                                    unsigned int feature) {
106
    return (ivs->features & (1 << feature));
107
}
108

    
109
static inline bool is_power_of_two(uint64_t x) {
110
    return (x & (x - 1)) == 0;
111
}
112

    
113
/* accessing registers - based on rtl8139 */
114
static void ivshmem_update_irq(IVShmemState *s, int val)
115
{
116
    int isr;
117
    isr = (s->intrstatus & s->intrmask) & 0xffffffff;
118

    
119
    /* don't print ISR resets */
120
    if (isr) {
121
        IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
122
           isr ? 1 : 0, s->intrstatus, s->intrmask);
123
    }
124

    
125
    qemu_set_irq(s->dev.irq[0], (isr != 0));
126
}
127

    
128
static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
129
{
130
    IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
131

    
132
    s->intrmask = val;
133

    
134
    ivshmem_update_irq(s, val);
135
}
136

    
137
static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
138
{
139
    uint32_t ret = s->intrmask;
140

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

    
143
    return ret;
144
}
145

    
146
static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
147
{
148
    IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
149

    
150
    s->intrstatus = val;
151

    
152
    ivshmem_update_irq(s, val);
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, hwaddr 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, hwaddr 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

    
515
/* Select the MSI-X vectors used by device.
516
 * ivshmem maps events to vectors statically, so
517
 * we just enable all vectors on init and after reset. */
518
static void ivshmem_use_msix(IVShmemState * s)
519
{
520
    int i;
521

    
522
    if (!msix_present(&s->dev)) {
523
        return;
524
    }
525

    
526
    for (i = 0; i < s->vectors; i++) {
527
        msix_vector_use(&s->dev, i);
528
    }
529
}
530

    
531
static void ivshmem_reset(DeviceState *d)
532
{
533
    IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
534

    
535
    s->intrstatus = 0;
536
    ivshmem_use_msix(s);
537
}
538

    
539
static uint64_t ivshmem_get_size(IVShmemState * s) {
540

    
541
    uint64_t value;
542
    char *ptr;
543

    
544
    value = strtoull(s->sizearg, &ptr, 10);
545
    switch (*ptr) {
546
        case 0: case 'M': case 'm':
547
            value <<= 20;
548
            break;
549
        case 'G': case 'g':
550
            value <<= 30;
551
            break;
552
        default:
553
            fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
554
            exit(1);
555
    }
556

    
557
    /* BARs must be a power of 2 */
558
    if (!is_power_of_two(value)) {
559
        fprintf(stderr, "ivshmem: size must be power of 2\n");
560
        exit(1);
561
    }
562

    
563
    return value;
564
}
565

    
566
static void ivshmem_setup_msi(IVShmemState * s)
567
{
568
    if (msix_init_exclusive_bar(&s->dev, s->vectors, 1)) {
569
        IVSHMEM_DPRINTF("msix initialization failed\n");
570
        exit(1);
571
    }
572

    
573
    IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
574

    
575
    /* allocate QEMU char devices for receiving interrupts */
576
    s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
577

    
578
    ivshmem_use_msix(s);
579
}
580

    
581
static void ivshmem_save(QEMUFile* f, void *opaque)
582
{
583
    IVShmemState *proxy = opaque;
584

    
585
    IVSHMEM_DPRINTF("ivshmem_save\n");
586
    pci_device_save(&proxy->dev, f);
587

    
588
    if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
589
        msix_save(&proxy->dev, f);
590
    } else {
591
        qemu_put_be32(f, proxy->intrstatus);
592
        qemu_put_be32(f, proxy->intrmask);
593
    }
594

    
595
}
596

    
597
static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
598
{
599
    IVSHMEM_DPRINTF("ivshmem_load\n");
600

    
601
    IVShmemState *proxy = opaque;
602
    int ret;
603

    
604
    if (version_id > 0) {
605
        return -EINVAL;
606
    }
607

    
608
    if (proxy->role_val == IVSHMEM_PEER) {
609
        fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
610
        return -EINVAL;
611
    }
612

    
613
    ret = pci_device_load(&proxy->dev, f);
614
    if (ret) {
615
        return ret;
616
    }
617

    
618
    if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
619
        msix_load(&proxy->dev, f);
620
        ivshmem_use_msix(proxy);
621
    } else {
622
        proxy->intrstatus = qemu_get_be32(f);
623
        proxy->intrmask = qemu_get_be32(f);
624
    }
625

    
626
    return 0;
627
}
628

    
629
static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
630
                                 uint32_t val, int len)
631
{
632
    pci_default_write_config(pci_dev, address, val, len);
633
    msix_write_config(pci_dev, address, val, len);
634
}
635

    
636
static int pci_ivshmem_init(PCIDevice *dev)
637
{
638
    IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
639
    uint8_t *pci_conf;
640

    
641
    if (s->sizearg == NULL)
642
        s->ivshmem_size = 4 << 20; /* 4 MB default */
643
    else {
644
        s->ivshmem_size = ivshmem_get_size(s);
645
    }
646

    
647
    register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
648
                                                                        dev);
649

    
650
    /* IRQFD requires MSI */
651
    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
652
        !ivshmem_has_feature(s, IVSHMEM_MSI)) {
653
        fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
654
        exit(1);
655
    }
656

    
657
    /* check that role is reasonable */
658
    if (s->role) {
659
        if (strncmp(s->role, "peer", 5) == 0) {
660
            s->role_val = IVSHMEM_PEER;
661
        } else if (strncmp(s->role, "master", 7) == 0) {
662
            s->role_val = IVSHMEM_MASTER;
663
        } else {
664
            fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
665
            exit(1);
666
        }
667
    } else {
668
        s->role_val = IVSHMEM_MASTER; /* default */
669
    }
670

    
671
    if (s->role_val == IVSHMEM_PEER) {
672
        error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
673
                  "peer mode", "ivshmem");
674
        migrate_add_blocker(s->migration_blocker);
675
    }
676

    
677
    pci_conf = s->dev.config;
678
    pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
679

    
680
    pci_config_set_interrupt_pin(pci_conf, 1);
681

    
682
    s->shm_fd = 0;
683

    
684
    memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
685
                          "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
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
    s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
693
        PCI_BASE_ADDRESS_MEM_PREFETCH;
694
    if (s->ivshmem_64bit) {
695
        s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
696
    }
697

    
698
    if ((s->server_chr != NULL) &&
699
                        (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
700
        /* if we get a UNIX socket as the parameter we will talk
701
         * to the ivshmem server to receive the memory region */
702

    
703
        if (s->shmobj != NULL) {
704
            fprintf(stderr, "WARNING: do not specify both 'chardev' "
705
                                                "and 'shm' with ivshmem\n");
706
        }
707

    
708
        IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
709
                                                    s->server_chr->filename);
710

    
711
        if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
712
            ivshmem_setup_msi(s);
713
        }
714

    
715
        /* we allocate enough space for 16 guests and grow as needed */
716
        s->nb_peers = 16;
717
        s->vm_id = -1;
718

    
719
        /* allocate/initialize space for interrupt handling */
720
        s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
721

    
722
        pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
723

    
724
        s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
725

    
726
        qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
727
                     ivshmem_event, s);
728
    } else {
729
        /* just map the file immediately, we're not using a server */
730
        int fd;
731

    
732
        if (s->shmobj == NULL) {
733
            fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
734
        }
735

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

    
738
        /* try opening with O_EXCL and if it succeeds zero the memory
739
         * by truncating to 0 */
740
        if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
741
                        S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
742
           /* truncate file to length PCI device's memory */
743
            if (ftruncate(fd, s->ivshmem_size) != 0) {
744
                fprintf(stderr, "ivshmem: could not truncate shared file\n");
745
            }
746

    
747
        } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
748
                        S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
749
            fprintf(stderr, "ivshmem: could not open shared file\n");
750
            exit(-1);
751

    
752
        }
753

    
754
        if (check_shm_size(s, fd) == -1) {
755
            exit(-1);
756
        }
757

    
758
        create_shared_memory_BAR(s, fd);
759

    
760
    }
761

    
762
    s->dev.config_write = ivshmem_write_config;
763

    
764
    return 0;
765
}
766

    
767
static void pci_ivshmem_uninit(PCIDevice *dev)
768
{
769
    IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
770

    
771
    if (s->migration_blocker) {
772
        migrate_del_blocker(s->migration_blocker);
773
        error_free(s->migration_blocker);
774
    }
775

    
776
    memory_region_destroy(&s->ivshmem_mmio);
777
    memory_region_del_subregion(&s->bar, &s->ivshmem);
778
    vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
779
    memory_region_destroy(&s->ivshmem);
780
    memory_region_destroy(&s->bar);
781
    unregister_savevm(&dev->qdev, "ivshmem", s);
782
}
783

    
784
static Property ivshmem_properties[] = {
785
    DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
786
    DEFINE_PROP_STRING("size", IVShmemState, sizearg),
787
    DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
788
    DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
789
    DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
790
    DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
791
    DEFINE_PROP_STRING("role", IVShmemState, role),
792
    DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
793
    DEFINE_PROP_END_OF_LIST(),
794
};
795

    
796
static void ivshmem_class_init(ObjectClass *klass, void *data)
797
{
798
    DeviceClass *dc = DEVICE_CLASS(klass);
799
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
800

    
801
    k->init = pci_ivshmem_init;
802
    k->exit = pci_ivshmem_uninit;
803
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
804
    k->device_id = 0x1110;
805
    k->class_id = PCI_CLASS_MEMORY_RAM;
806
    dc->reset = ivshmem_reset;
807
    dc->props = ivshmem_properties;
808
}
809

    
810
static TypeInfo ivshmem_info = {
811
    .name          = "ivshmem",
812
    .parent        = TYPE_PCI_DEVICE,
813
    .instance_size = sizeof(IVShmemState),
814
    .class_init    = ivshmem_class_init,
815
};
816

    
817
static void ivshmem_register_types(void)
818
{
819
    type_register_static(&ivshmem_info);
820
}
821

    
822
type_init(ivshmem_register_types)