static void xen_pt_region_update(XenPCIPassthroughState *s,

                                 MemoryRegionSection *sec, bool adding)

{

    PCIDevice *d = &s->dev;

    MemoryRegion *mr = sec->mr;

    int bar = -1;

    int rc;

    int op = adding ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING;

    struct CheckBarArgs args = {

        .s = s,

        .addr = sec->offset_within_address_space,

        .size = int128_get64(sec->size),

        .rc = false,

    };



    bar = xen_pt_bar_from_region(s, mr);

    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {

        return;

    }



    if (s->msix && &s->msix->mmio == mr) {

        if (adding) {

            s->msix->mmio_base_addr = sec->offset_within_address_space;

            rc = xen_pt_msix_update_remap(s, s->msix->bar_index);

        }

        return;

    }



    args.type = d->io_regions[bar].type;

    pci_for_each_device(d->bus, pci_bus_num(d->bus),

                        xen_pt_check_bar_overlap, &args);

    if (args.rc) {

        XEN_PT_WARN(d, "Region: %d (addr: %#"FMT_PCIBUS

                    ", len: %#"FMT_PCIBUS") is overlapped.\n",

                    bar, sec->offset_within_address_space,

                    int128_get64(sec->size));

    }



    if (d->io_regions[bar].type & PCI_BASE_ADDRESS_SPACE_IO) {

        uint32_t guest_port = sec->offset_within_address_space;

        uint32_t machine_port = s->bases[bar].access.pio_base;

        uint32_t size = int128_get64(sec->size);

        rc = xc_domain_ioport_mapping(xen_xc, xen_domid,

                                      guest_port, machine_port, size,

                                      op);

        if (rc) {

            XEN_PT_ERR(d, "%s ioport mapping failed! (err: %i)\n",

                       adding ? "create new" : "remove old", errno);

        }

    } else {

        pcibus_t guest_addr = sec->offset_within_address_space;

        pcibus_t machine_addr = s->bases[bar].access.maddr

            + sec->offset_within_region;

        pcibus_t size = int128_get64(sec->size);

        rc = xc_domain_memory_mapping(xen_xc, xen_domid,

                                      XEN_PFN(guest_addr + XC_PAGE_SIZE - 1),

                                      XEN_PFN(machine_addr + XC_PAGE_SIZE - 1),

                                      XEN_PFN(size + XC_PAGE_SIZE - 1),

                                      op);

        if (rc) {

            XEN_PT_ERR(d, "%s mem mapping failed! (err: %i)\n",

                       adding ? "create new" : "remove old", errno);

        }

    }

}

static void xen_pt_region_update(XenPCIPassthroughState *s,

                                 MemoryRegionSection *sec, bool adding)

{

    PCIDevice *d = &s->dev;

    MemoryRegion *mr = sec->mr;

    int bar = -1;

    int rc;

    int op = adding ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING;

    struct CheckBarArgs args = {

        .s = s,

        .addr = sec->offset_within_address_space,

        .size = int128_get64(sec->size),

        .rc = false,

    };



    bar = xen_pt_bar_from_region(s, mr);

    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {

        return;

    }



    if (s->msix && &s->msix->mmio == mr) {

        if (adding) {

            s->msix->mmio_base_addr = sec->offset_within_address_space;

            rc = xen_pt_msix_update_remap(s, s->msix->bar_index);

        }

        return;

    }



    args.type = d->io_regions[bar].type;

    pci_for_each_device(d->bus, pci_bus_num(d->bus),

                        xen_pt_check_bar_overlap, &args);

    if (args.rc) {

        XEN_PT_WARN(d, "Region: %d (addr: %#"FMT_PCIBUS

                    ", len: %#"FMT_PCIBUS") is overlapped.\n",

                    bar, sec->offset_within_address_space,

                    int128_get64(sec->size));

    }



    if (d->io_regions[bar].type & PCI_BASE_ADDRESS_SPACE_IO) {

        uint32_t guest_port = sec->offset_within_address_space;

        uint32_t machine_port = s->bases[bar].access.pio_base;

        uint32_t size = int128_get64(sec->size);

        rc = xc_domain_ioport_mapping(xen_xc, xen_domid,

                                      guest_port, machine_port, size,

                                      op);

        if (rc) {

            XEN_PT_ERR(d, "%s ioport mapping failed! (err: %i)\n",

                       adding ? "create new" : "remove old", errno);

        }

    } else {

        pcibus_t guest_addr = sec->offset_within_address_space;

        pcibus_t machine_addr = s->bases[bar].access.maddr

            + sec->offset_within_region;

        pcibus_t size = int128_get64(sec->size);

        rc = xc_domain_memory_mapping(xen_xc, xen_domid,

                                      XEN_PFN(guest_addr + XC_PAGE_SIZE - 1),

                                      XEN_PFN(machine_addr + XC_PAGE_SIZE - 1),

                                      XEN_PFN(size + XC_PAGE_SIZE - 1),

                                      op);

        if (rc) {

            XEN_PT_ERR(d, "%s mem mapping failed! (err: %i)\n",

                       adding ? "create new" : "remove old", errno);

        }

    }

}

static void FUNC_0(XenPCIPassthroughState *VAR_0,

                                 MemoryRegionSection *VAR_1, bool VAR_2)

{

    PCIDevice *d = &VAR_0->dev;

    MemoryRegion *mr = VAR_1->mr;

    int VAR_3 = -1;

    int VAR_4;

    int VAR_5 = VAR_2 ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING;

    struct CheckBarArgs VAR_6 = {

        .VAR_0 = VAR_0,

        .addr = VAR_1->offset_within_address_space,

        .size = int128_get64(VAR_1->size),

        .VAR_4 = false,

    };



    VAR_3 = xen_pt_bar_from_region(VAR_0, mr);

    if (VAR_3 == -1 && (!VAR_0->msix || &VAR_0->msix->mmio != mr)) {

        return;

    }



    if (VAR_0->msix && &VAR_0->msix->mmio == mr) {

        if (VAR_2) {

            VAR_0->msix->mmio_base_addr = VAR_1->offset_within_address_space;

            VAR_4 = xen_pt_msix_update_remap(VAR_0, VAR_0->msix->bar_index);

        }

        return;

    }



    VAR_6.type = d->io_regions[VAR_3].type;

    pci_for_each_device(d->bus, pci_bus_num(d->bus),

                        xen_pt_check_bar_overlap, &VAR_6);

    if (VAR_6.VAR_4) {

        XEN_PT_WARN(d, "Region: %d (addr: %#"FMT_PCIBUS

                    ", len: %#"FMT_PCIBUS") is overlapped.\n",

                    VAR_3, VAR_1->offset_within_address_space,

                    int128_get64(VAR_1->size));

    }



    if (d->io_regions[VAR_3].type & PCI_BASE_ADDRESS_SPACE_IO) {

        uint32_t guest_port = VAR_1->offset_within_address_space;

        uint32_t machine_port = VAR_0->bases[VAR_3].access.pio_base;

        uint32_t size = int128_get64(VAR_1->size);

        VAR_4 = xc_domain_ioport_mapping(xen_xc, xen_domid,

                                      guest_port, machine_port, size,

                                      VAR_5);

        if (VAR_4) {

            XEN_PT_ERR(d, "%VAR_0 ioport mapping failed! (err: %i)\n",

                       VAR_2 ? "create new" : "remove old", errno);

        }

    } else {

        pcibus_t guest_addr = VAR_1->offset_within_address_space;

        pcibus_t machine_addr = VAR_0->bases[VAR_3].access.maddr

            + VAR_1->offset_within_region;

        pcibus_t size = int128_get64(VAR_1->size);

        VAR_4 = xc_domain_memory_mapping(xen_xc, xen_domid,

                                      XEN_PFN(guest_addr + XC_PAGE_SIZE - 1),

                                      XEN_PFN(machine_addr + XC_PAGE_SIZE - 1),

                                      XEN_PFN(size + XC_PAGE_SIZE - 1),

                                      VAR_5);

        if (VAR_4) {

            XEN_PT_ERR(d, "%VAR_0 mem mapping failed! (err: %i)\n",

                       VAR_2 ? "create new" : "remove old", errno);

        }

    }

}

static void FUNC_0(XenPCIPassthroughState *VAR_0,
MemoryRegionSection *VAR_1, bool VAR_2)
{ PCIDevice *d = &VAR_0->dev; MemoryRegion *mr = VAR_1->mr; int VAR_3 = -1; int VAR_4; int VAR_5 = VAR_2 ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING; struct CheckBarArgs VAR_6 = { .VAR_0 = VAR_0,
.addr = VAR_1->offset_within_address_space,
.size = int128_get64(VAR_1->size),
.VAR_4 = false,
}; VAR_3 = xen_pt_bar_from_region(VAR_0, mr); if (VAR_3 == -1 && (!VAR_0->msix || &VAR_0->msix->mmio != mr)) { return; } if (VAR_0->msix && &VAR_0->msix->mmio == mr) { if (VAR_2) { VAR_0->msix->mmio_base_addr = VAR_1->offset_within_address_space; VAR_4 = xen_pt_msix_update_remap(VAR_0, VAR_0->msix->bar_index); } return; } VAR_6.type = d->io_regions[VAR_3].type; pci_for_each_device(d->bus, pci_bus_num(d->bus),
xen_pt_check_bar_overlap, &VAR_6); if (VAR_6.VAR_4) { XEN_PT_WARN(d, "Region: %d (addr: %#"FMT_PCIBUS
", len: %#"FMT_PCIBUS") is overlapped.\n",
VAR_3, VAR_1->offset_within_address_space,
int128_get64(VAR_1->size)); } if (d->io_regions[VAR_3].type & PCI_BASE_ADDRESS_SPACE_IO) { uint32_t guest_port = VAR_1->offset_within_address_space; uint32_t machine_port = VAR_0->bases[VAR_3].access.pio_base; uint32_t size = int128_get64(VAR_1->size); VAR_4 = xc_domain_ioport_mapping(xen_xc, xen_domid,
guest_port, machine_port, size,
VAR_5); if (VAR_4) { XEN_PT_ERR(d, "%VAR_0 ioport mapping failed! (err: %i)\n",
VAR_2 ? "create new" : "remove old", errno); } } else { pcibus_t guest_addr = VAR_1->offset_within_address_space; pcibus_t machine_addr = VAR_0->bases[VAR_3].access.maddr
+ VAR_1->offset_within_region; pcibus_t size = int128_get64(VAR_1->size); VAR_4 = xc_domain_memory_mapping(xen_xc, xen_domid,
XEN_PFN(guest_addr + XC_PAGE_SIZE - 1),
XEN_PFN(machine_addr + XC_PAGE_SIZE - 1),
XEN_PFN(size + XC_PAGE_SIZE - 1),
VAR_5); if (VAR_4) { XEN_PT_ERR(d, "%VAR_0 mem mapping failed! (err: %i)\n",
VAR_2 ? "create new" : "remove old", errno); } } }