static void ivshmem_read(void *opaque, const uint8_t *buf, int size)

{

    IVShmemState *s = opaque;

    int incoming_fd, tmp_fd;

    int guest_max_eventfd;

    long incoming_posn;



    if (fifo8_is_empty(&s->incoming_fifo) && size == sizeof(incoming_posn)) {

        memcpy(&incoming_posn, buf, size);

    } else {

        const uint8_t *p;

        uint32_t num;



        IVSHMEM_DPRINTF("short read of %d bytes\n", size);

        num = MAX(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo));

        fifo8_push_all(&s->incoming_fifo, buf, num);

        if (fifo8_num_used(&s->incoming_fifo) < sizeof(incoming_posn)) {

            return;

        }

        size -= num;

        buf += num;

        p = fifo8_pop_buf(&s->incoming_fifo, sizeof(incoming_posn), &num);

        g_assert(num == sizeof(incoming_posn));

        memcpy(&incoming_posn, p, sizeof(incoming_posn));

        if (size > 0) {

            fifo8_push_all(&s->incoming_fifo, buf, size);

        }

    }



    if (incoming_posn < -1) {

        IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn);

        return;

    }



    /* pick off s->server_chr->msgfd and store it, posn should accompany msg */

    tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);

    IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);



    /* make sure we have enough space for this guest */

    if (incoming_posn >= s->nb_peers) {

        if (increase_dynamic_storage(s, incoming_posn) < 0) {

            error_report("increase_dynamic_storage() failed");

            if (tmp_fd != -1) {


            }

            return;

        }

    }



    if (tmp_fd == -1) {

        /* if posn is positive and unseen before then this is our posn*/

        if ((incoming_posn >= 0) &&

                            (s->peers[incoming_posn].eventfds == NULL)) {

            /* receive our posn */

            s->vm_id = incoming_posn;

            return;

        } else {

            /* otherwise an fd == -1 means an existing guest has gone away */

            IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);

            close_guest_eventfds(s, incoming_posn);

            return;

        }

    }



    /* because of the implementation of get_msgfd, we need a dup */

    incoming_fd = dup(tmp_fd);



    if (incoming_fd == -1) {

        fprintf(stderr, "could not allocate file descriptor %s\n",

                                                            strerror(errno));


        return;

    }



    /* if the position is -1, then it's shared memory region fd */

    if (incoming_posn == -1) {



        void * map_ptr;



        s->max_peer = 0;



        if (check_shm_size(s, incoming_fd) == -1) {

            exit(-1);

        }



        /* mmap the region and map into the BAR2 */

        map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,

                                                            incoming_fd, 0);

        memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),

                                   "ivshmem.bar2", s->ivshmem_size, map_ptr);

        vmstate_register_ram(&s->ivshmem, DEVICE(s));



        IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",

                         map_ptr, s->ivshmem_size);



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



        /* only store the fd if it is successfully mapped */

        s->shm_fd = incoming_fd;



        return;

    }



    /* each guest has an array of eventfds, and we keep track of how many

     * guests for each VM */

    guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;



    if (guest_max_eventfd == 0) {

        /* one eventfd per MSI vector */

        s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);

    }



    /* this is an eventfd for a particular guest VM */

    IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,

                                            guest_max_eventfd, incoming_fd);

    event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],

                           incoming_fd);



    /* increment count for particular guest */

    s->peers[incoming_posn].nb_eventfds++;



    /* keep track of the maximum VM ID */

    if (incoming_posn > s->max_peer) {

        s->max_peer = incoming_posn;

    }



    if (incoming_posn == s->vm_id) {

        s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,

                   &s->peers[s->vm_id].eventfds[guest_max_eventfd],

                   guest_max_eventfd);

    }



    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {

        ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);

    }

}
static void ivshmem_read(void *opaque, const uint8_t *buf, int size)

{

    IVShmemState *s = opaque;

    int incoming_fd, tmp_fd;

    int guest_max_eventfd;

    long incoming_posn;



    if (fifo8_is_empty(&s->incoming_fifo) && size == sizeof(incoming_posn)) {

        memcpy(&incoming_posn, buf, size);

    } else {

        const uint8_t *p;

        uint32_t num;



        IVSHMEM_DPRINTF("short read of %d bytes\n", size);

        num = MAX(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo));

        fifo8_push_all(&s->incoming_fifo, buf, num);

        if (fifo8_num_used(&s->incoming_fifo) < sizeof(incoming_posn)) {

            return;

        }

        size -= num;

        buf += num;

        p = fifo8_pop_buf(&s->incoming_fifo, sizeof(incoming_posn), &num);

        g_assert(num == sizeof(incoming_posn));

        memcpy(&incoming_posn, p, sizeof(incoming_posn));

        if (size > 0) {

            fifo8_push_all(&s->incoming_fifo, buf, size);

        }

    }



    if (incoming_posn < -1) {

        IVSHMEM_DPRINTF("invalid incoming_posn %ld\n", incoming_posn);

        return;

    }



    

    tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);

    IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);



    

    if (incoming_posn >= s->nb_peers) {

        if (increase_dynamic_storage(s, incoming_posn) < 0) {

            error_report("increase_dynamic_storage() failed");

            if (tmp_fd != -1) {


            }

            return;

        }

    }



    if (tmp_fd == -1) {

        

        if ((incoming_posn >= 0) &&

                            (s->peers[incoming_posn].eventfds == NULL)) {

            

            s->vm_id = incoming_posn;

            return;

        } else {

            

            IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);

            close_guest_eventfds(s, incoming_posn);

            return;

        }

    }



    

    incoming_fd = dup(tmp_fd);



    if (incoming_fd == -1) {

        fprintf(stderr, "could not allocate file descriptor %s\n",

                                                            strerror(errno));


        return;

    }



    

    if (incoming_posn == -1) {



        void * map_ptr;



        s->max_peer = 0;



        if (check_shm_size(s, incoming_fd) == -1) {

            exit(-1);

        }



        

        map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,

                                                            incoming_fd, 0);

        memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),

                                   "ivshmem.bar2", s->ivshmem_size, map_ptr);

        vmstate_register_ram(&s->ivshmem, DEVICE(s));



        IVSHMEM_DPRINTF("guest h/w addr = %p, size = %" PRIu64 "\n",

                         map_ptr, s->ivshmem_size);



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



        

        s->shm_fd = incoming_fd;



        return;

    }



    



    guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;



    if (guest_max_eventfd == 0) {

        

        s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);

    }



    

    IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,

                                            guest_max_eventfd, incoming_fd);

    event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],

                           incoming_fd);



    

    s->peers[incoming_posn].nb_eventfds++;



    

    if (incoming_posn > s->max_peer) {

        s->max_peer = incoming_posn;

    }



    if (incoming_posn == s->vm_id) {

        s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,

                   &s->peers[s->vm_id].eventfds[guest_max_eventfd],

                   guest_max_eventfd);

    }



    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {

        ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);

    }

}
static void FUNC_0(void *VAR_0, const uint8_t *VAR_1, int VAR_2)

{

    IVShmemState *s = VAR_0;

    int VAR_3, VAR_4;

    int VAR_5;

    long VAR_6;



    if (fifo8_is_empty(&s->incoming_fifo) && VAR_2 == sizeof(VAR_6)) {

        memcpy(&VAR_6, VAR_1, VAR_2);

    } else {

        const uint8_t *VAR_7;

        uint32_t num;



        IVSHMEM_DPRINTF("short read of %d bytes\n", VAR_2);

        num = MAX(VAR_2, sizeof(long) - fifo8_num_used(&s->incoming_fifo));

        fifo8_push_all(&s->incoming_fifo, VAR_1, num);

        if (fifo8_num_used(&s->incoming_fifo) < sizeof(VAR_6)) {

            return;

        }

        VAR_2 -= num;

        VAR_1 += num;

        VAR_7 = fifo8_pop_buf(&s->incoming_fifo, sizeof(VAR_6), &num);

        g_assert(num == sizeof(VAR_6));

        memcpy(&VAR_6, VAR_7, sizeof(VAR_6));

        if (VAR_2 > 0) {

            fifo8_push_all(&s->incoming_fifo, VAR_1, VAR_2);

        }

    }



    if (VAR_6 < -1) {

        IVSHMEM_DPRINTF("invalid VAR_6 %ld\n", VAR_6);

        return;

    }



    

    VAR_4 = qemu_chr_fe_get_msgfd(s->server_chr);

    IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", VAR_6, VAR_4);



    

    if (VAR_6 >= s->nb_peers) {

        if (increase_dynamic_storage(s, VAR_6) < 0) {

            error_report("increase_dynamic_storage() failed");

            if (VAR_4 != -1) {


            }

            return;

        }

    }



    if (VAR_4 == -1) {

        

        if ((VAR_6 >= 0) &&

                            (s->peers[VAR_6].eventfds == NULL)) {

            

            s->vm_id = VAR_6;

            return;

        } else {

            

            IVSHMEM_DPRINTF("posn %ld has gone away\n", VAR_6);

            close_guest_eventfds(s, VAR_6);

            return;

        }

    }



    

    VAR_3 = dup(VAR_4);



    if (VAR_3 == -1) {

        fprintf(stderr, "could not allocate file descriptor %s\n",

                                                            strerror(errno));


        return;

    }



    

    if (VAR_6 == -1) {



        void * VAR_8;



        s->max_peer = 0;



        if (check_shm_size(s, VAR_3) == -1) {

            exit(-1);

        }



        

        VAR_8 = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,

                                                            VAR_3, 0);

        memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),

                                   "ivshmem.bar2", s->ivshmem_size, VAR_8);

        vmstate_register_ram(&s->ivshmem, DEVICE(s));



        IVSHMEM_DPRINTF("guest h/w addr = %VAR_7, VAR_2 = %" PRIu64 "\n",

                         VAR_8, s->ivshmem_size);



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



        

        s->shm_fd = VAR_3;



        return;

    }



    



    VAR_5 = s->peers[VAR_6].nb_eventfds;



    if (VAR_5 == 0) {

        

        s->peers[VAR_6].eventfds = g_new(EventNotifier, s->vectors);

    }



    

    IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", VAR_6,

                                            VAR_5, VAR_3);

    event_notifier_init_fd(&s->peers[VAR_6].eventfds[VAR_5],

                           VAR_3);



    

    s->peers[VAR_6].nb_eventfds++;



    

    if (VAR_6 > s->max_peer) {

        s->max_peer = VAR_6;

    }



    if (VAR_6 == s->vm_id) {

        s->eventfd_chr[VAR_5] = create_eventfd_chr_device(s,

                   &s->peers[s->vm_id].eventfds[VAR_5],

                   VAR_5);

    }



    if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {

        ivshmem_add_eventfd(s, VAR_6, VAR_5);

    }

}
static void FUNC_0(void *VAR_0, const uint8_t *VAR_1, int VAR_2)
{ IVShmemState *s = VAR_0; int VAR_3, VAR_4; int VAR_5; long VAR_6; if (fifo8_is_empty(&s->incoming_fifo) && VAR_2 == sizeof(VAR_6)) { memcpy(&VAR_6, VAR_1, VAR_2); } else { const uint8_t *VAR_7; uint32_t num; IVSHMEM_DPRINTF("short read of %d bytes\n", VAR_2); num = MAX(VAR_2, sizeof(long) - fifo8_num_used(&s->incoming_fifo)); fifo8_push_all(&s->incoming_fifo, VAR_1, num); if (fifo8_num_used(&s->incoming_fifo) < sizeof(VAR_6)) { return; } VAR_2 -= num; VAR_1 += num; VAR_7 = fifo8_pop_buf(&s->incoming_fifo, sizeof(VAR_6), &num); g_assert(num == sizeof(VAR_6)); memcpy(&VAR_6, VAR_7, sizeof(VAR_6)); if (VAR_2 > 0) { fifo8_push_all(&s->incoming_fifo, VAR_1, VAR_2); } } if (VAR_6 < -1) { IVSHMEM_DPRINTF("invalid VAR_6 %ld\n", VAR_6); return; } VAR_4 = qemu_chr_fe_get_msgfd(s->server_chr); IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", VAR_6, VAR_4); if (VAR_6 >= s->nb_peers) { if (increase_dynamic_storage(s, VAR_6) < 0) { error_report("increase_dynamic_storage() failed"); if (VAR_4 != -1) { } return; } } if (VAR_4 == -1) { if ((VAR_6 >= 0) &&
(s->peers[VAR_6].eventfds == NULL)) { s->vm_id = VAR_6; return; } else { IVSHMEM_DPRINTF("posn %ld has gone away\n", VAR_6); close_guest_eventfds(s, VAR_6); return; } } VAR_3 = dup(VAR_4); if (VAR_3 == -1) { fprintf(stderr, "could not allocate file descriptor %s\n",
strerror(errno)); return; } if (VAR_6 == -1) { void * VAR_8; s->max_peer = 0; if (check_shm_size(s, VAR_3) == -1) { exit(-1); } VAR_8 = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
VAR_3, 0); memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s),
"ivshmem.bar2", s->ivshmem_size, VAR_8); vmstate_register_ram(&s->ivshmem, DEVICE(s)); IVSHMEM_DPRINTF("guest h/w addr = %VAR_7, VAR_2 = %" PRIu64 "\n",
VAR_8, s->ivshmem_size); memory_region_add_subregion(&s->bar, 0, &s->ivshmem); s->shm_fd = VAR_3; return; } VAR_5 = s->peers[VAR_6].nb_eventfds; if (VAR_5 == 0) { s->peers[VAR_6].eventfds = g_new(EventNotifier, s->vectors); } IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", VAR_6,
VAR_5, VAR_3); event_notifier_init_fd(&s->peers[VAR_6].eventfds[VAR_5],
VAR_3); s->peers[VAR_6].nb_eventfds++; if (VAR_6 > s->max_peer) { s->max_peer = VAR_6; } if (VAR_6 == s->vm_id) { s->eventfd_chr[VAR_5] = create_eventfd_chr_device(s,
&s->peers[s->vm_id].eventfds[VAR_5],
VAR_5); } if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) { ivshmem_add_eventfd(s, VAR_6, VAR_5); } }