From: "Jan Beulich" Subject: [PATCH] x86/MSI: add mechanism to protect MSI-X table from PV guest accesses Date: Wed, 06 Feb 2013 16:50:45 +0000 Message-ID: <511297F502000078000BC935@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part576719F5.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part576719F5.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This adds two new physdev operations for Dom0 to invoke when resource allocation for devices is known to be complete, so that the hypervisor can arrange for the respective MMIO ranges to be marked read-only before an eventual guest getting such a device assigned even gets started, such that it won't be able to set up writable mappings for these MMIO ranges before Xen has a chance to protect them. This also addresses another issue with the code being modified here, in that so far write protection for the address ranges in question got set up only once during the lifetime of a device (i.e. until either system shutdown or device hot removal), while teardown happened when the last interrupt was disposed of by the guest (which at least allowed the tables to be writable when the device got assigned to a second guest [instance] after the first terminated). Signed-off-by: Jan Beulich --- a/xen/arch/x86/msi.c +++ b/xen/arch/x86/msi.c @@ -656,8 +656,8 @@ static u64 read_pci_mem_bar(u16 seg, u8=20 * @entries: pointer to an array of struct msix_entry entries * @nvec: number of @entries * - * Setup the MSI-X capability structure of device function with a - * single MSI-X irq. A return of zero indicates the successful setup of + * Setup the MSI-X capability structure of device function with the = requested + * number MSI-X irqs. A return of zero indicates the successful setup of * requested MSI-X entries with allocated irqs or non-zero for otherwise. **/ static int msix_capability_init(struct pci_dev *dev, @@ -665,86 +665,69 @@ static int msix_capability_init(struct p struct msi_desc **desc, unsigned int nr_entries) { - struct msi_desc *entry; - int pos; + struct msi_desc *entry =3D NULL; + int pos, vf; u16 control; - u64 table_paddr, entry_paddr; - u32 table_offset, entry_offset; - u8 bir; - void __iomem *base; - int idx; + u64 table_paddr; + u32 table_offset; + u8 bir, pbus, pslot, pfunc; u16 seg =3D dev->seg; u8 bus =3D dev->bus; u8 slot =3D PCI_SLOT(dev->devfn); u8 func =3D PCI_FUNC(dev->devfn); =20 ASSERT(spin_is_locked(&pcidevs_lock)); - ASSERT(desc); =20 pos =3D pci_find_cap_offset(seg, bus, slot, func, PCI_CAP_ID_MSIX); control =3D pci_conf_read16(seg, bus, slot, func, msix_control_reg(pos= )); msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */ =20 - /* MSI-X Table Initialization */ - entry =3D alloc_msi_entry(); - if ( !entry ) - return -ENOMEM; + if ( desc ) + { + entry =3D alloc_msi_entry(); + if ( !entry ) + return -ENOMEM; + ASSERT(msi); + } =20 - /* Request & Map MSI-X table region */ + /* Locate MSI-X table region */ table_offset =3D pci_conf_read32(seg, bus, slot, func, msix_table_offset_reg(pos)); bir =3D (u8)(table_offset & PCI_MSIX_BIRMASK); table_offset &=3D ~PCI_MSIX_BIRMASK; - entry_offset =3D msi->entry_nr * PCI_MSIX_ENTRY_SIZE; =20 - table_paddr =3D msi->table_base + table_offset; - entry_paddr =3D table_paddr + entry_offset; - idx =3D msix_get_fixmap(dev, table_paddr, entry_paddr); - if ( idx < 0 ) - { - xfree(entry); - return idx; - } - base =3D (void *)(fix_to_virt(idx) + - ((unsigned long)entry_paddr & ((1UL << PAGE_SHIFT) - 1))); - - entry->msi_attrib.type =3D PCI_CAP_ID_MSIX; - entry->msi_attrib.is_64 =3D 1; - entry->msi_attrib.entry_nr =3D msi->entry_nr; - entry->msi_attrib.maskbit =3D 1; - entry->msi_attrib.masked =3D 1; - entry->msi_attrib.pos =3D pos; - entry->irq =3D msi->irq; - entry->dev =3D dev; - entry->mask_base =3D base; - - list_add_tail(&entry->list, &dev->msi_list); - - if ( !dev->msix_nr_entries ) + if ( !dev->info.is_virtfn ) { - u8 pbus, pslot, pfunc; - int vf; - u64 pba_paddr; - u32 pba_offset; + pbus =3D bus; + pslot =3D slot; + pfunc =3D func; + vf =3D -1; + } + else + { + pbus =3D dev->info.physfn.bus; + pslot =3D PCI_SLOT(dev->info.physfn.devfn); + pfunc =3D PCI_FUNC(dev->info.physfn.devfn); + vf =3D PCI_BDF2(dev->bus, dev->devfn); + } =20 - if ( !dev->info.is_virtfn ) - { - pbus =3D bus; - pslot =3D slot; - pfunc =3D func; - vf =3D -1; - } - else + table_paddr =3D read_pci_mem_bar(seg, pbus, pslot, pfunc, bir, vf); + WARN_ON(msi && msi->table_base !=3D table_paddr); + if ( !table_paddr ) + { + if ( !msi || !msi->table_base ) { - pbus =3D dev->info.physfn.bus; - pslot =3D PCI_SLOT(dev->info.physfn.devfn); - pfunc =3D PCI_FUNC(dev->info.physfn.devfn); - vf =3D PCI_BDF2(dev->bus, dev->devfn); + xfree(entry); + return -ENXIO; } + table_paddr =3D msi->table_base; + } + table_paddr +=3D table_offset; =20 - ASSERT(!dev->msix_used_entries); - WARN_ON(msi->table_base !=3D - read_pci_mem_bar(seg, pbus, pslot, pfunc, bir, vf)); + if ( !dev->msix_used_entries ) + { + u64 pba_paddr; + u32 pba_offset; =20 dev->msix_nr_entries =3D nr_entries; dev->msix_table.first =3D PFN_DOWN(table_paddr); @@ -765,7 +748,42 @@ static int msix_capability_init(struct p BITS_TO_LONGS(nr_entries) - 1); WARN_ON(rangeset_overlaps_range(mmio_ro_ranges, dev->msix_pba.firs= t, dev->msix_pba.last)); + } =20 + if ( entry ) + { + /* Map MSI-X table region */ + u64 entry_paddr =3D table_paddr + msi->entry_nr * PCI_MSIX_ENTRY_S= IZE; + int idx =3D msix_get_fixmap(dev, table_paddr, entry_paddr); + void __iomem *base; + + if ( idx < 0 ) + { + xfree(entry); + return idx; + } + base =3D (void *)(fix_to_virt(idx) + + ((unsigned long)entry_paddr & (PAGE_SIZE - 1))); + + /* Mask interrupt here */ + writel(1, base + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET); + + entry->msi_attrib.type =3D PCI_CAP_ID_MSIX; + entry->msi_attrib.is_64 =3D 1; + entry->msi_attrib.entry_nr =3D msi->entry_nr; + entry->msi_attrib.maskbit =3D 1; + entry->msi_attrib.masked =3D 1; + entry->msi_attrib.pos =3D pos; + entry->irq =3D msi->irq; + entry->dev =3D dev; + entry->mask_base =3D base; + + list_add_tail(&entry->list, &dev->msi_list); + *desc =3D entry; + } + + if ( !dev->msix_used_entries ) + { if ( rangeset_add_range(mmio_ro_ranges, dev->msix_table.first, dev->msix_table.last) ) WARN(); @@ -776,7 +794,7 @@ static int msix_capability_init(struct p if ( dev->domain ) p2m_change_entry_type_global(dev->domain, p2m_mmio_direct, p2m_mmio_direct)= ; - if ( !dev->domain || !paging_mode_translate(dev->domain) ) + if ( desc && (!dev->domain || !paging_mode_translate(dev->domain))= ) { struct domain *d =3D dev->domain; =20 @@ -790,6 +808,13 @@ static int msix_capability_init(struct p break; if ( d ) { + if ( !IS_PRIV(d) && dev->msix_warned !=3D d->domain_id ) + { + dev->msix_warned =3D d->domain_id; + printk(XENLOG_ERR + "Potentially insecure use of MSI-X on = %04x:%02x:%02x.%u by Dom%d\n", + seg, bus, slot, func, d->domain_id); + } /* XXX How to deal with existing mappings? */ } } @@ -798,10 +823,6 @@ static int msix_capability_init(struct p WARN_ON(dev->msix_table.first !=3D (table_paddr >> PAGE_SHIFT)); ++dev->msix_used_entries; =20 - /* Mask interrupt here */ - writel(1, entry->mask_base + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET); - - *desc =3D entry; /* Restore MSI-X enabled bits */ pci_conf_write16(seg, bus, slot, func, msix_control_reg(pos), = control); =20 @@ -926,6 +947,19 @@ static int __pci_enable_msix(struct msi_ return status; } =20 +static void _pci_cleanup_msix(struct pci_dev *dev) +{ + if ( !--dev->msix_used_entries ) + { + if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_table.first, + dev->msix_table.last) ) + WARN(); + if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_pba.first, + dev->msix_pba.last) ) + WARN(); + } +} + static void __pci_disable_msix(struct msi_desc *entry) { struct pci_dev *dev; @@ -949,15 +983,42 @@ static void __pci_disable_msix(struct ms =20 pci_conf_write16(seg, bus, slot, func, msix_control_reg(pos), = control); =20 - if ( !--dev->msix_used_entries ) + _pci_cleanup_msix(dev); +} + +int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off) +{ + int rc; + struct pci_dev *pdev; + u8 slot =3D PCI_SLOT(devfn), func =3D PCI_FUNC(devfn); + unsigned int pos =3D pci_find_cap_offset(seg, bus, slot, func, + PCI_CAP_ID_MSIX); + + if ( !pos ) + return -ENODEV; + + spin_lock(&pcidevs_lock); + pdev =3D pci_get_pdev(seg, bus, devfn); + if ( !pdev ) + rc =3D -ENODEV; + else if ( pdev->msix_used_entries !=3D !!off ) + rc =3D -EBUSY; + else if ( off ) { - if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_table.first, - dev->msix_table.last) ) - WARN(); - if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_pba.first, - dev->msix_pba.last) ) - WARN(); + _pci_cleanup_msix(pdev); + rc =3D 0; } + else + { + u16 control =3D pci_conf_read16(seg, bus, slot, func, + msix_control_reg(pos)); + + rc =3D msix_capability_init(pdev, NULL, NULL, + multi_msix_capable(control)); + } + spin_unlock(&pcidevs_lock); + + return rc; } =20 /* --- a/xen/arch/x86/physdev.c +++ b/xen/arch/x86/physdev.c @@ -575,6 +575,18 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H break; } =20 + case PHYSDEVOP_prepare_msix: + case PHYSDEVOP_release_msix: { + struct physdev_pci_device dev; + + if ( copy_from_guest(&dev, arg, 1) ) + ret =3D -EFAULT; + else + ret =3D pci_prepare_msix(dev.seg, dev.bus, dev.devfn, + cmd !=3D PHYSDEVOP_prepare_msix); + break; + } + case PHYSDEVOP_pci_mmcfg_reserved: { struct physdev_pci_mmcfg_reserved info; =20 --- a/xen/include/asm-x86/msi.h +++ b/xen/include/asm-x86/msi.h @@ -76,6 +76,7 @@ struct msi_desc; /* Helper functions */ extern int pci_enable_msi(struct msi_info *msi, struct msi_desc **desc); extern void pci_disable_msi(struct msi_desc *desc); +extern int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off); extern void pci_cleanup_msi(struct pci_dev *pdev); extern void setup_msi_handler(struct irq_desc *, struct msi_desc *); extern void setup_msi_irq(struct irq_desc *); --- a/xen/include/public/physdev.h +++ b/xen/include/public/physdev.h @@ -303,6 +303,12 @@ DEFINE_XEN_GUEST_HANDLE(physdev_pci_devi =20 #define PHYSDEVOP_pci_device_remove 26 #define PHYSDEVOP_restore_msi_ext 27 +/* + * Dom0 should use these two to announce MMIO resources assigned to + * MSI-X capable devices won't (prepare) or may (release) change. + */ +#define PHYSDEVOP_prepare_msix 30 +#define PHYSDEVOP_release_msix 31 struct physdev_pci_device { /* IN */ uint16_t seg; --- a/xen/include/xen/pci.h +++ b/xen/include/xen/pci.h @@ -57,6 +57,7 @@ struct pci_dev { int msix_table_refcnt[MAX_MSIX_TABLE_PAGES]; int msix_table_idx[MAX_MSIX_TABLE_PAGES]; spinlock_t msix_table_lock; + domid_t msix_warned; =20 struct domain *domain; const u16 seg; --=__Part576719F5.0__= Content-Type: text/plain; name="x86-MSI-X-fully-hide-table.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-MSI-X-fully-hide-table.patch" x86/MSI: add mechanism to protect MSI-X table from PV guest accesses=0A=0AT= his adds two new physdev operations for Dom0 to invoke when resource=0Aallo= cation for devices is known to be complete, so that the hypervisor=0Acan = arrange for the respective MMIO ranges to be marked read-only=0Abefore an = eventual guest getting such a device assigned even gets=0Astarted, such = that it won't be able to set up writable mappings for=0Athese MMIO ranges = before Xen has a chance to protect them.=0A=0AThis also addresses another = issue with the code being modified here,=0Ain that so far write protection = for the address ranges in question got=0Aset up only once during the = lifetime of a device (i.e. until either=0Asystem shutdown or device hot = removal), while teardown happened when=0Athe last interrupt was disposed = of by the guest (which at least allowed=0Athe tables to be writable when = the device got assigned to a second=0Aguest [instance] after the first = terminated).=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/msi.c=0A+++ b/xen/arch/x86/msi.c=0A@@ -656,8 +656,8 @@ = static u64 read_pci_mem_bar(u16 seg, u8 =0A * @entries: pointer to an = array of struct msix_entry entries=0A * @nvec: number of @entries=0A = *=0A- * Setup the MSI-X capability structure of device function with a=0A- = * single MSI-X irq. A return of zero indicates the successful setup of=0A+ = * Setup the MSI-X capability structure of device function with the = requested=0A+ * number MSI-X irqs. A return of zero indicates the = successful setup of=0A * requested MSI-X entries with allocated irqs or = non-zero for otherwise.=0A **/=0A static int msix_capability_init(struct = pci_dev *dev,=0A@@ -665,86 +665,69 @@ static int msix_capability_init(struc= t p=0A struct msi_desc **desc,=0A = unsigned int nr_entries)=0A {=0A- struct = msi_desc *entry;=0A- int pos;=0A+ struct msi_desc *entry =3D = NULL;=0A+ int pos, vf;=0A u16 control;=0A- u64 table_paddr, = entry_paddr;=0A- u32 table_offset, entry_offset;=0A- u8 bir;=0A- = void __iomem *base;=0A- int idx;=0A+ u64 table_paddr;=0A+ u32 = table_offset;=0A+ u8 bir, pbus, pslot, pfunc;=0A u16 seg =3D = dev->seg;=0A u8 bus =3D dev->bus;=0A u8 slot =3D PCI_SLOT(dev->devf= n);=0A u8 func =3D PCI_FUNC(dev->devfn);=0A =0A ASSERT(spin_is_lock= ed(&pcidevs_lock));=0A- ASSERT(desc);=0A =0A pos =3D pci_find_cap_of= fset(seg, bus, slot, func, PCI_CAP_ID_MSIX);=0A control =3D pci_conf_re= ad16(seg, bus, slot, func, msix_control_reg(pos));=0A msix_set_enable(d= ev, 0);/* Ensure msix is disabled as I set it up */=0A =0A- /* MSI-X = Table Initialization */=0A- entry =3D alloc_msi_entry();=0A- if ( = !entry )=0A- return -ENOMEM;=0A+ if ( desc )=0A+ {=0A+ = entry =3D alloc_msi_entry();=0A+ if ( !entry )=0A+ = return -ENOMEM;=0A+ ASSERT(msi);=0A+ }=0A =0A- /* Request & = Map MSI-X table region */=0A+ /* Locate MSI-X table region */=0A = table_offset =3D pci_conf_read32(seg, bus, slot, func,=0A = msix_table_offset_reg(pos));=0A bir =3D (u8)(table_of= fset & PCI_MSIX_BIRMASK);=0A table_offset &=3D ~PCI_MSIX_BIRMASK;=0A- = entry_offset =3D msi->entry_nr * PCI_MSIX_ENTRY_SIZE;=0A =0A- = table_paddr =3D msi->table_base + table_offset;=0A- entry_paddr =3D = table_paddr + entry_offset;=0A- idx =3D msix_get_fixmap(dev, table_paddr= , entry_paddr);=0A- if ( idx < 0 )=0A- {=0A- xfree(entry);=0A-= return idx;=0A- }=0A- base =3D (void *)(fix_to_virt(idx) = +=0A- ((unsigned long)entry_paddr & ((1UL << PAGE_SHIFT) - = 1)));=0A-=0A- entry->msi_attrib.type =3D PCI_CAP_ID_MSIX;=0A- = entry->msi_attrib.is_64 =3D 1;=0A- entry->msi_attrib.entry_nr =3D = msi->entry_nr;=0A- entry->msi_attrib.maskbit =3D 1;=0A- entry->msi_at= trib.masked =3D 1;=0A- entry->msi_attrib.pos =3D pos;=0A- entry->irq = =3D msi->irq;=0A- entry->dev =3D dev;=0A- entry->mask_base =3D = base;=0A-=0A- list_add_tail(&entry->list, &dev->msi_list);=0A-=0A- = if ( !dev->msix_nr_entries )=0A+ if ( !dev->info.is_virtfn )=0A = {=0A- u8 pbus, pslot, pfunc;=0A- int vf;=0A- u64 = pba_paddr;=0A- u32 pba_offset;=0A+ pbus =3D bus;=0A+ = pslot =3D slot;=0A+ pfunc =3D func;=0A+ vf =3D -1;=0A+ = }=0A+ else=0A+ {=0A+ pbus =3D dev->info.physfn.bus;=0A+ = pslot =3D PCI_SLOT(dev->info.physfn.devfn);=0A+ pfunc =3D = PCI_FUNC(dev->info.physfn.devfn);=0A+ vf =3D PCI_BDF2(dev->bus, = dev->devfn);=0A+ }=0A =0A- if ( !dev->info.is_virtfn )=0A- = {=0A- pbus =3D bus;=0A- pslot =3D slot;=0A- = pfunc =3D func;=0A- vf =3D -1;=0A- }=0A- = else=0A+ table_paddr =3D read_pci_mem_bar(seg, pbus, pslot, pfunc, bir, = vf);=0A+ WARN_ON(msi && msi->table_base !=3D table_paddr);=0A+ if ( = !table_paddr )=0A+ {=0A+ if ( !msi || !msi->table_base )=0A = {=0A- pbus =3D dev->info.physfn.bus;=0A- pslot = =3D PCI_SLOT(dev->info.physfn.devfn);=0A- pfunc =3D PCI_FUNC(dev= ->info.physfn.devfn);=0A- vf =3D PCI_BDF2(dev->bus, dev->devfn);= =0A+ xfree(entry);=0A+ return -ENXIO;=0A = }=0A+ table_paddr =3D msi->table_base;=0A+ }=0A+ table_paddr = +=3D table_offset;=0A =0A- ASSERT(!dev->msix_used_entries);=0A- = WARN_ON(msi->table_base !=3D=0A- read_pci_mem_bar(seg, = pbus, pslot, pfunc, bir, vf));=0A+ if ( !dev->msix_used_entries )=0A+ = {=0A+ u64 pba_paddr;=0A+ u32 pba_offset;=0A =0A = dev->msix_nr_entries =3D nr_entries;=0A dev->msix_table.first =3D = PFN_DOWN(table_paddr);=0A@@ -765,7 +748,42 @@ static int msix_capability_in= it(struct p=0A BITS_TO_LONGS(nr_entri= es) - 1);=0A WARN_ON(rangeset_overlaps_range(mmio_ro_ranges, = dev->msix_pba.first,=0A dev->msix_p= ba.last));=0A+ }=0A =0A+ if ( entry )=0A+ {=0A+ /* Map = MSI-X table region */=0A+ u64 entry_paddr =3D table_paddr + = msi->entry_nr * PCI_MSIX_ENTRY_SIZE;=0A+ int idx =3D msix_get_fixmap= (dev, table_paddr, entry_paddr);=0A+ void __iomem *base;=0A+=0A+ = if ( idx < 0 )=0A+ {=0A+ xfree(entry);=0A+ = return idx;=0A+ }=0A+ base =3D (void *)(fix_to_virt(idx) = +=0A+ ((unsigned long)entry_paddr & (PAGE_SIZE - = 1)));=0A+=0A+ /* Mask interrupt here */=0A+ writel(1, base + = PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);=0A+=0A+ entry->msi_attrib.type = =3D PCI_CAP_ID_MSIX;=0A+ entry->msi_attrib.is_64 =3D 1;=0A+ = entry->msi_attrib.entry_nr =3D msi->entry_nr;=0A+ entry->msi_attrib.= maskbit =3D 1;=0A+ entry->msi_attrib.masked =3D 1;=0A+ = entry->msi_attrib.pos =3D pos;=0A+ entry->irq =3D msi->irq;=0A+ = entry->dev =3D dev;=0A+ entry->mask_base =3D base;=0A+=0A+ = list_add_tail(&entry->list, &dev->msi_list);=0A+ *desc =3D = entry;=0A+ }=0A+=0A+ if ( !dev->msix_used_entries )=0A+ {=0A = if ( rangeset_add_range(mmio_ro_ranges, dev->msix_table.first,=0A = dev->msix_table.last) )=0A WARN();=0A= @@ -776,7 +794,7 @@ static int msix_capability_init(struct p=0A if = ( dev->domain )=0A p2m_change_entry_type_global(dev->domain,=0A= p2m_mmio_direct, p2m_mmio_direct)= ;=0A- if ( !dev->domain || !paging_mode_translate(dev->domain) = )=0A+ if ( desc && (!dev->domain || !paging_mode_translate(dev->doma= in)) )=0A {=0A struct domain *d =3D dev->domain;=0A = =0A@@ -790,6 +808,13 @@ static int msix_capability_init(struct p=0A = break;=0A if ( d )=0A {=0A+ = if ( !IS_PRIV(d) && dev->msix_warned !=3D d->domain_id )=0A+ = {=0A+ dev->msix_warned =3D d->domain_id;=0A+ = printk(XENLOG_ERR=0A+ = "Potentially insecure use of MSI-X on %04x:%02x:%02x.%u by Dom%d\n",=0A+ = seg, bus, slot, func, d->domain_id);=0A+ = }=0A /* XXX How to deal with existing mappings? */=0A = }=0A }=0A@@ -798,10 +823,6 @@ static int msix_capabilit= y_init(struct p=0A WARN_ON(dev->msix_table.first !=3D (table_paddr >> = PAGE_SHIFT));=0A ++dev->msix_used_entries;=0A =0A- /* Mask = interrupt here */=0A- writel(1, entry->mask_base + PCI_MSIX_ENTRY_VECTOR= _CTRL_OFFSET);=0A-=0A- *desc =3D entry;=0A /* Restore MSI-X enabled = bits */=0A pci_conf_write16(seg, bus, slot, func, msix_control_reg(pos)= , control);=0A =0A@@ -926,6 +947,19 @@ static int __pci_enable_msix(struct = msi_=0A return status;=0A }=0A =0A+static void _pci_cleanup_msix(struct= pci_dev *dev)=0A+{=0A+ if ( !--dev->msix_used_entries )=0A+ {=0A+ = if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_table.first,=0A+ = dev->msix_table.last) )=0A+ = WARN();=0A+ if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_pba= .first,=0A+ dev->msix_pba.last) )=0A+ = WARN();=0A+ }=0A+}=0A+=0A static void __pci_disable_msix(struct = msi_desc *entry)=0A {=0A struct pci_dev *dev;=0A@@ -949,15 +983,42 @@ = static void __pci_disable_msix(struct ms=0A =0A pci_conf_write16(seg, = bus, slot, func, msix_control_reg(pos), control);=0A =0A- if ( = !--dev->msix_used_entries )=0A+ _pci_cleanup_msix(dev);=0A+}=0A+=0A+int = pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off)=0A+{=0A+ int = rc;=0A+ struct pci_dev *pdev;=0A+ u8 slot =3D PCI_SLOT(devfn), func = =3D PCI_FUNC(devfn);=0A+ unsigned int pos =3D pci_find_cap_offset(seg, = bus, slot, func,=0A+ PCI_CAP_ID_M= SIX);=0A+=0A+ if ( !pos )=0A+ return -ENODEV;=0A+=0A+ = spin_lock(&pcidevs_lock);=0A+ pdev =3D pci_get_pdev(seg, bus, devfn);=0A= + if ( !pdev )=0A+ rc =3D -ENODEV;=0A+ else if ( pdev->msix_us= ed_entries !=3D !!off )=0A+ rc =3D -EBUSY;=0A+ else if ( off = )=0A {=0A- if ( rangeset_remove_range(mmio_ro_ranges, dev->msix_= table.first,=0A- dev->msix_table.last) = )=0A- WARN();=0A- if ( rangeset_remove_range(mmio_ro_rang= es, dev->msix_pba.first,=0A- dev->msix_pb= a.last) )=0A- WARN();=0A+ _pci_cleanup_msix(pdev);=0A+ = rc =3D 0;=0A }=0A+ else=0A+ {=0A+ u16 control =3D = pci_conf_read16(seg, bus, slot, func,=0A+ = msix_control_reg(pos));=0A+=0A+ rc =3D msix_capability_init(pdev= , NULL, NULL,=0A+ multi_msix_capable(contr= ol));=0A+ }=0A+ spin_unlock(&pcidevs_lock);=0A+=0A+ return rc;=0A = }=0A =0A /*=0A--- a/xen/arch/x86/physdev.c=0A+++ b/xen/arch/x86/physdev.c= =0A@@ -575,6 +575,18 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H=0A = break;=0A }=0A =0A+ case PHYSDEVOP_prepare_msix:=0A+ case = PHYSDEVOP_release_msix: {=0A+ struct physdev_pci_device dev;=0A+=0A+= if ( copy_from_guest(&dev, arg, 1) )=0A+ ret =3D = -EFAULT;=0A+ else=0A+ ret =3D pci_prepare_msix(dev.seg, = dev.bus, dev.devfn,=0A+ cmd !=3D = PHYSDEVOP_prepare_msix);=0A+ break;=0A+ }=0A+=0A case = PHYSDEVOP_pci_mmcfg_reserved: {=0A struct physdev_pci_mmcfg_reserve= d info;=0A =0A--- a/xen/include/asm-x86/msi.h=0A+++ b/xen/include/asm-x86/m= si.h=0A@@ -76,6 +76,7 @@ struct msi_desc;=0A /* Helper functions */=0A = extern int pci_enable_msi(struct msi_info *msi, struct msi_desc **desc);=0A= extern void pci_disable_msi(struct msi_desc *desc);=0A+extern int = pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off);=0A extern void = pci_cleanup_msi(struct pci_dev *pdev);=0A extern void setup_msi_handler(str= uct irq_desc *, struct msi_desc *);=0A extern void setup_msi_irq(struct = irq_desc *);=0A--- a/xen/include/public/physdev.h=0A+++ b/xen/include/publi= c/physdev.h=0A@@ -303,6 +303,12 @@ DEFINE_XEN_GUEST_HANDLE(physdev_pci_devi= =0A =0A #define PHYSDEVOP_pci_device_remove 26=0A #define PHYSDEVOP_res= tore_msi_ext 27=0A+/*=0A+ * Dom0 should use these two to announce = MMIO resources assigned to=0A+ * MSI-X capable devices won't (prepare) or = may (release) change.=0A+ */=0A+#define PHYSDEVOP_prepare_msix = 30=0A+#define PHYSDEVOP_release_msix 31=0A struct physdev_pci_devi= ce {=0A /* IN */=0A uint16_t seg;=0A--- a/xen/include/xen/pci.h=0A+= ++ b/xen/include/xen/pci.h=0A@@ -57,6 +57,7 @@ struct pci_dev {=0A int = msix_table_refcnt[MAX_MSIX_TABLE_PAGES];=0A int msix_table_idx[MAX_MSIX= _TABLE_PAGES];=0A spinlock_t msix_table_lock;=0A+ domid_t msix_warne= d;=0A =0A struct domain *domain;=0A const u16 seg;=0A --=__Part576719F5.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part576719F5.0__=--