From: "Jan Beulich" Subject: Re: [PATCH V7 11/11] xen passthrough: clean up MSI-X table handling Date: Tue, 21 Feb 2012 09:34:02 +0000 Message-ID: <4F43731A02000078000740BA@nat28.tlf.novell.com> References: <1329498525-8454-1-git-send-email-anthony.perard@citrix.com> <1329498525-8454-12-git-send-email-anthony.perard@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <1329498525-8454-12-git-send-email-anthony.perard@citrix.com> Content-Disposition: inline List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+gceq-qemu-devel=gmane.org@nongnu.org Sender: qemu-devel-bounces+gceq-qemu-devel=gmane.org@nongnu.org To: Anthony PERARD Cc: Shan Haitao , Xen Devel , QEMU-devel , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org >>> On 17.02.12 at 18:08, Anthony PERARD = wrote: Wouldn't thus much better be merged into the prior patch(es)? After all you're not trying to reconstruct the Xen-side history of this code anyway. Jan > From: Shan Haitao >=20 > This patch does cleaning up of QEMU MSI handling. The fixes are: > 1. Changes made to MSI-X table mapping handling to eliminate the small > windows in which guest could have access to physical MSI-X table. > 2. MSI-X table is mapped as read-only to QEMU, as masking of MSI-X is > already in Xen now. > 3. For registers that coexists inside the MSI-X table (this could be > only PBA I think), value read from physical page would be returned. >=20 > Signed-off-by: Shan Haitao >=20 > Consolidated duplicate code into _pt_iomem_helper(). Fixed formatting. >=20 > Signed-off-by: Jan Beulich > Signed-off-by: Anthony PERARD > --- > hw/xen_pci_passthrough.c | 80 ++++++++++++++++++++++++++++++++----= ------ > hw/xen_pci_passthrough.h | 11 +++++- > hw/xen_pci_passthrough_msi.c | 62 ++++---------------------------- > 3 files changed, 78 insertions(+), 75 deletions(-) >=20 > diff --git a/hw/xen_pci_passthrough.c b/hw/xen_pci_passthrough.c > index 6ea2c45..fab9fbe 100644 > --- a/hw/xen_pci_passthrough.c > +++ b/hw/xen_pci_passthrough.c > @@ -356,6 +356,53 @@ out: > } > } > =20 > +static int pt_iomem_helper(XenPCIPassthroughState *s, int i, > + pcibus_t e_base, pcibus_t e_size, int op) > +{ > + uint64_t msix_last_pfn =3D 0; > + pcibus_t bar_last_pfn =3D 0; > + pcibus_t msix_table_size =3D 0; > + XenPTMSIX *msix =3D s->msix; > + int rc =3D 0; > + > + if (!pt_has_msix_mapping(s, i)) { > + return xc_domain_memory_mapping(xen_xc, xen_domid, > + PFN(e_base), > + PFN(s->bases[i].access.maddr), > + PFN(e_size + XC_PAGE_SIZE - 1), > + op); > + > + } > + > + if (msix->table_off) { > + rc =3D xc_domain_memory_mapping(xen_xc, xen_domid, > + PFN(e_base), > + PFN(s->bases[i].access.maddr), > + PFN(msix->mmio_base_addr) - = PFN(e_base), > + op); > + } > + > + msix_table_size =3D msix->total_entries * PCI_MSIX_ENTRY_SIZE; > + msix_last_pfn =3D PFN(msix->mmio_base_addr + msix_table_size - 1); > + bar_last_pfn =3D PFN(e_base + e_size - 1); > + > + if (rc =3D=3D 0 && msix_last_pfn !=3D bar_last_pfn) { > + assert(msix_last_pfn < bar_last_pfn); > + > + rc =3D xc_domain_memory_mapping(xen_xc, xen_domid, > + msix_last_pfn + 1, > + PFN(s->bases[i].access.maddr > + + msix->table_off > + + msix_table_size > + + XC_PAGE_SIZE - 1), > + bar_last_pfn - msix_last_pfn, > + op); > + } > + > + return rc; > +} > + > + > /* ioport/iomem space*/ > static void pt_iomem_map(XenPCIPassthroughState *s, int i, > pcibus_t e_phys, pcibus_t e_size) > @@ -376,13 +423,10 @@ static void pt_iomem_map(XenPCIPassthroughState = *s, int=20 > i, > } > =20 > if (!first_map && old_ebase !=3D PT_PCI_BAR_UNMAPPED) { > - pt_add_msix_mapping(s, i); > - /* Remove old mapping */ > - rc =3D xc_domain_memory_mapping(xen_xc, xen_domid, > - old_ebase >> XC_PAGE_SHIFT, > - s->bases[i].access.maddr >> XC_PAGE_SHIFT= , > - (e_size + XC_PAGE_SIZE - 1) >> XC_PAGE_SH= IFT, > - DPCI_REMOVE_MAPPING); > + if (pt_has_msix_mapping(s, i)) { > + memory_region_set_enabled(&s->msix->mmio, false); > + } > + rc =3D pt_iomem_helper(s, i, old_ebase, e_size, DPCI_REMOVE_MAPP= ING); > if (rc) { > PT_ERR(&s->dev, "remove old mapping failed! (rc: %i)\n", = rc); > return; > @@ -391,21 +435,19 @@ static void pt_iomem_map(XenPCIPassthroughState = *s, int=20 > i, > =20 > /* map only valid guest address */ > if (e_phys !=3D PCI_BAR_UNMAPPED) { > - /* Create new mapping */ > - rc =3D xc_domain_memory_mapping(xen_xc, xen_domid, > - s->bases[i].e_physbase >> XC_PAGE_SHI= FT, > - s->bases[i].access.maddr >> = XC_PAGE_SHIFT, > - (e_size+XC_PAGE_SIZE-1) >> XC_PAGE_SH= IFT, > - DPCI_ADD_MAPPING); > + if (pt_has_msix_mapping(s, i)) { > + XenPTMSIX *msix =3D s->msix; > =20 > - if (rc) { > - PT_ERR(&s->dev, "create new mapping failed! (rc: %i)\n", = rc); > + msix->mmio_base_addr =3D s->bases[i].e_physbase + msix->tabl= e_off; > + > + memory_region_set_enabled(&msix->mmio, true); > } > =20 > - rc =3D pt_remove_msix_mapping(s, i); > - if (rc !=3D 0) { > - PT_ERR(&s->dev, "Remove MSI-X MMIO mapping failed! (rc: = %d)\n", > - rc); > + rc =3D pt_iomem_helper(s, i, e_phys, e_size, DPCI_ADD_MAPPING); > + > + if (rc) { > + PT_ERR(&s->dev, "create new mapping failed! (rc: %i)\n", = rc); > + return; > } > =20 > if (old_ebase !=3D e_phys && old_ebase !=3D -1) { > diff --git a/hw/xen_pci_passthrough.h b/hw/xen_pci_passthrough.h > index 3d41e04..f36e0d2 100644 > --- a/hw/xen_pci_passthrough.h > +++ b/hw/xen_pci_passthrough.h > @@ -30,6 +30,9 @@ void pt_log(const PCIDevice *d, const char *f, ...)=20 > GCC_FMT_ATTR(2, 3); > #endif > =20 > =20 > +/* Helper */ > +#define PFN(x) ((x) >> XC_PAGE_SHIFT) > + > typedef struct XenPTRegInfo XenPTRegInfo; > typedef struct XenPTReg XenPTReg; > =20 > @@ -295,7 +298,11 @@ void pt_msix_delete(XenPCIPassthroughState *s); > int pt_msix_update(XenPCIPassthroughState *s); > int pt_msix_update_remap(XenPCIPassthroughState *s, int bar_index); > void pt_msix_disable(XenPCIPassthroughState *s); > -int pt_add_msix_mapping(XenPCIPassthroughState *s, int bar_index); > -int pt_remove_msix_mapping(XenPCIPassthroughState *s, int bar_index); > + > +static inline bool pt_has_msix_mapping(XenPCIPassthroughState *s, int = bar) > +{ > + return s->msix && s->msix->bar_index =3D=3D bar; > +} > + > =20 > #endif /* !QEMU_HW_XEN_PCI_PASSTHROUGH_H */ > diff --git a/hw/xen_pci_passthrough_msi.c b/hw/xen_pci_passthrough_msi.c > index e848c61..e775d21 100644 > --- a/hw/xen_pci_passthrough_msi.c > +++ b/hw/xen_pci_passthrough_msi.c > @@ -300,16 +300,6 @@ static int msix_set_enable(XenPCIPassthroughState = *s,=20 > bool enabled) > enabled); > } > =20 > -static void mask_physical_msix_entry(XenPCIPassthroughState *s, > - int entry_nr, int mask) > -{ > - void *phys_off; > - > - phys_off =3D s->msix->phys_iomem_base + PCI_MSIX_ENTRY_SIZE * = entry_nr > - + PCI_MSIX_ENTRY_VECTOR_CTRL; > - *(uint32_t *)phys_off =3D mask; > -} > - > static int pt_msix_update_one(XenPCIPassthroughState *s, int entry_nr) > { > XenPTMSIXEntry *entry =3D NULL; > @@ -480,8 +470,6 @@ static void pci_msix_write(void *opaque,=20 > target_phys_addr_t addr, > if (msix->enabled && !(val & PCI_MSIX_ENTRY_CTRL_MASKBIT)) { > pt_msix_update_one(s, entry_nr); > } > - mask_physical_msix_entry(s, entry_nr, > - entry->vector_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT); > } > } > =20 > @@ -493,14 +481,19 @@ static uint64_t pci_msix_read(void *opaque,=20 > target_phys_addr_t addr, > int entry_nr, offset; > =20 > entry_nr =3D addr / PCI_MSIX_ENTRY_SIZE; > - if (entry_nr < 0 || entry_nr >=3D msix->total_entries) { > + if (entry_nr < 0) { > PT_ERR(&s->dev, "asked MSI-X entry '%i' invalid!\n", entry_nr); > return 0; > } > =20 > offset =3D addr % PCI_MSIX_ENTRY_SIZE; > =20 > - return get_entry_value(&msix->msix_entry[entry_nr], offset); > + if (addr < msix->total_entries * PCI_MSIX_ENTRY_SIZE) { > + return get_entry_value(&msix->msix_entry[entry_nr], offset); > + } else { > + /* Pending Bit Array (PBA) */ > + return *(uint32_t *)(msix->phys_iomem_base + addr); > + } > } > =20 > static const MemoryRegionOps pci_msix_ops =3D { > @@ -514,45 +507,6 @@ static const MemoryRegionOps pci_msix_ops =3D { > }, > }; > =20 > -int pt_add_msix_mapping(XenPCIPassthroughState *s, int bar_index) > -{ > - XenPTMSIX *msix =3D s->msix; > - > - if (!(s->msix && s->msix->bar_index =3D=3D bar_index)) { > - return 0; > - } > - > - memory_region_set_enabled(&msix->mmio, false); > - return xc_domain_memory_mapping(xen_xc, xen_domid, > - s->msix->mmio_base_addr >> XC_PAGE_SHIFT, > - (s->bases[bar_index].access.maddr + s->msix->table_off) > - >> XC_PAGE_SHIFT, > - (s->msix->total_entries * PCI_MSIX_ENTRY_SIZE + XC_PAGE_SIZE - = 1) > - >> XC_PAGE_SHIFT, > - DPCI_ADD_MAPPING); > -} > - > -int pt_remove_msix_mapping(XenPCIPassthroughState *s, int bar_index) > -{ > - XenPTMSIX *msix =3D s->msix; > - > - if (!(msix && msix->bar_index =3D=3D bar_index)) { > - return 0; > - } > - > - memory_region_set_enabled(&msix->mmio, true); > - > - msix->mmio_base_addr =3D s->bases[bar_index].e_physbase + msix->tabl= e_off; > - > - return xc_domain_memory_mapping(xen_xc, xen_domid, > - s->msix->mmio_base_addr >> XC_PAGE_SHIFT, > - (s->bases[bar_index].access.maddr + s->msix->table_off) > - >> XC_PAGE_SHIFT, > - (s->msix->total_entries * PCI_MSIX_ENTRY_SIZE + XC_PAGE_SIZE - = 1) > - >> XC_PAGE_SHIFT, > - DPCI_REMOVE_MAPPING); > -} > - > int pt_msix_init(XenPCIPassthroughState *s, uint32_t base) > { > uint8_t id =3D 0; > @@ -609,7 +563,7 @@ int pt_msix_init(XenPCIPassthroughState *s, = uint32_t=20 > base) > msix->phys_iomem_base =3D > mmap(NULL, > total_entries * PCI_MSIX_ENTRY_SIZE + msix->table_offset_ad= just, > - PROT_WRITE | PROT_READ, > + PROT_READ, > MAP_SHARED | MAP_LOCKED, > fd, > msix->table_base + table_off - msix->table_offset_adjust); > --=20 > Anthony PERARD