int vfio_region_mmap(VFIORegion *region)

{

    int i, prot = 0;

    char *name;



    if (!region->mem) {

        return 0;

    }



    prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;

    prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;



    for (i = 0; i < region->nr_mmaps; i++) {

        region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,

                                     MAP_SHARED, region->vbasedev->fd,

                                     region->fd_offset +

                                     region->mmaps[i].offset);

        if (region->mmaps[i].mmap == MAP_FAILED) {

            int ret = -errno;



            trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,

                                         region->fd_offset +

                                         region->mmaps[i].offset,

                                         region->fd_offset +

                                         region->mmaps[i].offset +

                                         region->mmaps[i].size - 1, ret);



            region->mmaps[i].mmap = NULL;



            for (i--; i >= 0; i--) {

                memory_region_del_subregion(region->mem, &region->mmaps[i].mem);

                munmap(region->mmaps[i].mmap, region->mmaps[i].size);

                object_unparent(OBJECT(&region->mmaps[i].mem));

                region->mmaps[i].mmap = NULL;

            }



            return ret;

        }



        name = g_strdup_printf("%s mmaps[%d]",

                               memory_region_name(region->mem), i);

        memory_region_init_ram_ptr(&region->mmaps[i].mem,

                                   memory_region_owner(region->mem),

                                   name, region->mmaps[i].size,

                                   region->mmaps[i].mmap);

        g_free(name);

        memory_region_set_skip_dump(&region->mmaps[i].mem);

        memory_region_add_subregion(region->mem, region->mmaps[i].offset,

                                    &region->mmaps[i].mem);



        trace_vfio_region_mmap(memory_region_name(&region->mmaps[i].mem),

                               region->mmaps[i].offset,

                               region->mmaps[i].offset +

                               region->mmaps[i].size - 1);

    }



    return 0;

}

int vfio_region_mmap(VFIORegion *region)

{

    int i, prot = 0;

    char *name;



    if (!region->mem) {

        return 0;

    }



    prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;

    prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;



    for (i = 0; i < region->nr_mmaps; i++) {

        region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,

                                     MAP_SHARED, region->vbasedev->fd,

                                     region->fd_offset +

                                     region->mmaps[i].offset);

        if (region->mmaps[i].mmap == MAP_FAILED) {

            int ret = -errno;



            trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,

                                         region->fd_offset +

                                         region->mmaps[i].offset,

                                         region->fd_offset +

                                         region->mmaps[i].offset +

                                         region->mmaps[i].size - 1, ret);



            region->mmaps[i].mmap = NULL;



            for (i--; i >= 0; i--) {

                memory_region_del_subregion(region->mem, &region->mmaps[i].mem);

                munmap(region->mmaps[i].mmap, region->mmaps[i].size);

                object_unparent(OBJECT(&region->mmaps[i].mem));

                region->mmaps[i].mmap = NULL;

            }



            return ret;

        }



        name = g_strdup_printf("%s mmaps[%d]",

                               memory_region_name(region->mem), i);

        memory_region_init_ram_ptr(&region->mmaps[i].mem,

                                   memory_region_owner(region->mem),

                                   name, region->mmaps[i].size,

                                   region->mmaps[i].mmap);

        g_free(name);

        memory_region_set_skip_dump(&region->mmaps[i].mem);

        memory_region_add_subregion(region->mem, region->mmaps[i].offset,

                                    &region->mmaps[i].mem);



        trace_vfio_region_mmap(memory_region_name(&region->mmaps[i].mem),

                               region->mmaps[i].offset,

                               region->mmaps[i].offset +

                               region->mmaps[i].size - 1);

    }



    return 0;

}

int FUNC_0(VFIORegion *VAR_0)

{

    int VAR_1, VAR_2 = 0;

    char *VAR_3;



    if (!VAR_0->mem) {

        return 0;

    }



    VAR_2 |= VAR_0->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;

    VAR_2 |= VAR_0->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;



    for (VAR_1 = 0; VAR_1 < VAR_0->nr_mmaps; VAR_1++) {

        VAR_0->mmaps[VAR_1].mmap = mmap(NULL, VAR_0->mmaps[VAR_1].size, VAR_2,

                                     MAP_SHARED, VAR_0->vbasedev->fd,

                                     VAR_0->fd_offset +

                                     VAR_0->mmaps[VAR_1].offset);

        if (VAR_0->mmaps[VAR_1].mmap == MAP_FAILED) {

            int ret = -errno;



            trace_vfio_region_mmap_fault(memory_region_name(VAR_0->mem), VAR_1,

                                         VAR_0->fd_offset +

                                         VAR_0->mmaps[VAR_1].offset,

                                         VAR_0->fd_offset +

                                         VAR_0->mmaps[VAR_1].offset +

                                         VAR_0->mmaps[VAR_1].size - 1, ret);



            VAR_0->mmaps[VAR_1].mmap = NULL;



            for (VAR_1--; VAR_1 >= 0; VAR_1--) {

                memory_region_del_subregion(VAR_0->mem, &VAR_0->mmaps[VAR_1].mem);

                munmap(VAR_0->mmaps[VAR_1].mmap, VAR_0->mmaps[VAR_1].size);

                object_unparent(OBJECT(&VAR_0->mmaps[VAR_1].mem));

                VAR_0->mmaps[VAR_1].mmap = NULL;

            }



            return ret;

        }



        VAR_3 = g_strdup_printf("%s mmaps[%d]",

                               memory_region_name(VAR_0->mem), VAR_1);

        memory_region_init_ram_ptr(&VAR_0->mmaps[VAR_1].mem,

                                   memory_region_owner(VAR_0->mem),

                                   VAR_3, VAR_0->mmaps[VAR_1].size,

                                   VAR_0->mmaps[VAR_1].mmap);

        g_free(VAR_3);

        memory_region_set_skip_dump(&VAR_0->mmaps[VAR_1].mem);

        memory_region_add_subregion(VAR_0->mem, VAR_0->mmaps[VAR_1].offset,

                                    &VAR_0->mmaps[VAR_1].mem);



        trace_vfio_region_mmap(memory_region_name(&VAR_0->mmaps[VAR_1].mem),

                               VAR_0->mmaps[VAR_1].offset,

                               VAR_0->mmaps[VAR_1].offset +

                               VAR_0->mmaps[VAR_1].size - 1);

    }



    return 0;

}

int FUNC_0(VFIORegion *VAR_0)
{ int VAR_1, VAR_2 = 0; char *VAR_3; if (!VAR_0->mem) { return 0; } VAR_2 |= VAR_0->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0; VAR_2 |= VAR_0->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0; for (VAR_1 = 0; VAR_1 < VAR_0->nr_mmaps; VAR_1++) { VAR_0->mmaps[VAR_1].mmap = mmap(NULL, VAR_0->mmaps[VAR_1].size, VAR_2,
MAP_SHARED, VAR_0->vbasedev->fd,
VAR_0->fd_offset +
VAR_0->mmaps[VAR_1].offset); if (VAR_0->mmaps[VAR_1].mmap == MAP_FAILED) { int ret = -errno; trace_vfio_region_mmap_fault(memory_region_name(VAR_0->mem), VAR_1,
VAR_0->fd_offset +
VAR_0->mmaps[VAR_1].offset,
VAR_0->fd_offset +
VAR_0->mmaps[VAR_1].offset +
VAR_0->mmaps[VAR_1].size - 1, ret); VAR_0->mmaps[VAR_1].mmap = NULL; for (VAR_1--; VAR_1 >= 0; VAR_1--) { memory_region_del_subregion(VAR_0->mem, &VAR_0->mmaps[VAR_1].mem); munmap(VAR_0->mmaps[VAR_1].mmap, VAR_0->mmaps[VAR_1].size); object_unparent(OBJECT(&VAR_0->mmaps[VAR_1].mem)); VAR_0->mmaps[VAR_1].mmap = NULL; } return ret; } VAR_3 = g_strdup_printf("%s mmaps[%d]",
memory_region_name(VAR_0->mem), VAR_1); memory_region_init_ram_ptr(&VAR_0->mmaps[VAR_1].mem,
memory_region_owner(VAR_0->mem),
VAR_3, VAR_0->mmaps[VAR_1].size,
VAR_0->mmaps[VAR_1].mmap); g_free(VAR_3); memory_region_set_skip_dump(&VAR_0->mmaps[VAR_1].mem); memory_region_add_subregion(VAR_0->mem, VAR_0->mmaps[VAR_1].offset,
&VAR_0->mmaps[VAR_1].mem); trace_vfio_region_mmap(memory_region_name(&VAR_0->mmaps[VAR_1].mem),
VAR_0->mmaps[VAR_1].offset,
VAR_0->mmaps[VAR_1].offset +
VAR_0->mmaps[VAR_1].size - 1); } return 0; }