From: "Jan Beulich" Subject: [PATCH] PCI: don't allow guest assignment of devices used by Xen Date: Tue, 11 Sep 2012 12:33:39 +0100 Message-ID: <504F3DB3020000780009A8CB@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartD5E4C783.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. --=__PartD5E4C783.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This covers the devices used for the console and the AMD IOMMU ones (as would be any others that might get passed to pci_ro_device()). Boot video device determination cloned from similar Linux logic. Signed-off-by: Jan Beulich --- Note that to apply cleanly, this has to go on top of the serial console improvement series posted earlier. --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -311,9 +311,12 @@ void __init arch_init_memory(void) * Initialise our DOMID_XEN domain. * Any Xen-heap pages that we will allow to be mapped will have * their domain field set to dom_xen. + * Hidden PCI devices will also be associated with this domain + * (but be [partly] controlled by Dom0 nevertheless). */ dom_xen =3D domain_create(DOMID_XEN, DOMCRF_dummy, 0); BUG_ON(IS_ERR(dom_xen)); + INIT_LIST_HEAD(&dom_xen->arch.pdev_list); =20 /* * Initialise our DOMID_IO domain. --- a/xen/drivers/char/ehci-dbgp.c +++ b/xen/drivers/char/ehci-dbgp.c @@ -1364,6 +1364,8 @@ static void __init ehci_dbgp_init_postir init_timer(&dbgp->timer, ehci_dbgp_poll, port, 0); =20 ehci_dbgp_setup_postirq(dbgp); + + pci_hide_device(dbgp->bus, PCI_DEVFN(dbgp->slot, dbgp->func)); } =20 static int ehci_dbgp_check_release(struct ehci_dbgp *dbgp) --- a/xen/drivers/char/ns16550.c +++ b/xen/drivers/char/ns16550.c @@ -334,6 +334,10 @@ static void __init ns16550_init_postirq( } =20 ns16550_setup_postirq(uart); + + if ( uart->bar || uart->ps_bdf_enable ) + pci_hide_device(uart->ps_bdf[0], PCI_DEVFN(uart->ps_bdf[1], + uart->ps_bdf[2])); } =20 static void ns16550_suspend(struct serial_port *port) --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -208,7 +208,7 @@ static int device_assigned(u16 seg, u8 b pdev =3D pci_get_pdev_by_domain(dom0, seg, bus, devfn); spin_unlock(&pcidevs_lock); =20 - return pdev ? 0 : -1; + return pdev ? 0 : -EBUSY; } =20 static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn) @@ -614,7 +614,8 @@ int iommu_do_domctl( bus =3D (domctl->u.assign_device.machine_sbdf >> 8) & 0xff; devfn =3D domctl->u.assign_device.machine_sbdf & 0xff; =20 - ret =3D assign_device(d, seg, bus, devfn); + ret =3D device_assigned(seg, bus, devfn) ?: + assign_device(d, seg, bus, devfn); if ( ret ) printk(XENLOG_G_ERR "XEN_DOMCTL_assign_device: " "assign %04x:%02x:%02x.%u to dom%d failed (%d)\n", --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -208,6 +208,31 @@ static void free_pdev(struct pci_seg *ps xfree(pdev); } =20 +static void _pci_hide_device(struct pci_dev *pdev) +{ + if ( pdev->domain ) + return; + pdev->domain =3D dom_xen; + list_add(&pdev->domain_list, &dom_xen->arch.pdev_list); +} + +int __init pci_hide_device(int bus, int devfn) +{ + struct pci_dev *pdev; + int rc =3D -ENOMEM; + + spin_lock(&pcidevs_lock); + pdev =3D alloc_pdev(get_pseg(0), bus, devfn); + if ( pdev ) + { + _pci_hide_device(pdev); + rc =3D 0; + } + spin_unlock(&pcidevs_lock); + + return rc; +} + int __init pci_ro_device(int seg, int bus, int devfn) { struct pci_seg *pseg =3D alloc_pseg(seg); @@ -231,6 +256,7 @@ int __init pci_ro_device(int seg, int bu =20 __set_bit(PCI_BDF2(bus, devfn), pseg->ro_map); arch_pci_ro_device(seg, PCI_BDF2(bus, devfn)); + _pci_hide_device(pdev); =20 return 0; } @@ -718,9 +744,22 @@ static int __init _setup_dom0_pci_device if ( !pdev ) continue; =20 - pdev->domain =3D ctxt->d; - list_add(&pdev->domain_list, &ctxt->d->arch.pdev_list); - ctxt->handler(pdev); + if ( !pdev->domain ) + { + pdev->domain =3D ctxt->d; + list_add(&pdev->domain_list, &ctxt->d->arch.pdev_list); + ctxt->handler(pdev); + } + else if ( pdev->domain =3D=3D dom_xen ) + { + pdev->domain =3D ctxt->d; + ctxt->handler(pdev); + pdev->domain =3D dom_xen; + } + else if ( pdev->domain !=3D ctxt->d ) + printk(XENLOG_WARNING "Dom%d owning %04x:%02x:%02x.%u?\n",= + pdev->domain->domain_id, pseg->nr, bus, + PCI_SLOT(devfn), PCI_FUNC(devfn)); } } =20 --- a/xen/drivers/video/vga.c +++ b/xen/drivers/video/vga.c @@ -9,6 +9,7 @@ #include #include #include +#include #include =20 /* Filled in by arch boot code. */ @@ -106,6 +107,61 @@ void __init vga_endboot(void) =20 if ( !vgacon_keep ) vga_puts =3D vga_noop_puts; + else + { + int bus, devfn; + + for ( bus =3D 0; bus < 256; ++bus ) + for ( devfn =3D 0; devfn < 256; ++devfn ) + { + const struct pci_dev *pdev; + u8 b =3D bus, df =3D devfn, sb; + + spin_lock(&pcidevs_lock); + pdev =3D pci_get_pdev(0, bus, devfn); + spin_unlock(&pcidevs_lock); + + if ( !pdev || + pci_conf_read16(0, bus, PCI_SLOT(devfn), PCI_FUNC(dev= fn), + PCI_CLASS_DEVICE) !=3D 0x0300 || + !(pci_conf_read16(0, bus, PCI_SLOT(devfn), + PCI_FUNC(devfn), PCI_COMMAND) & + (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) ) + continue; + + while ( b ) + { + switch ( find_upstream_bridge(0, &b, &df, &sb) ) + { + case 0: + b =3D 0; + break; + case 1: + switch ( pci_conf_read8(0, b, PCI_SLOT(df), + PCI_FUNC(df), + PCI_HEADER_TYPE) ) + { + case PCI_HEADER_TYPE_BRIDGE: + case PCI_HEADER_TYPE_CARDBUS: + if ( pci_conf_read16(0, b, PCI_SLOT(df), + PCI_FUNC(df), + PCI_BRIDGE_CONTROL) & + PCI_BRIDGE_CTL_VGA ) + continue; + break; + } + break; + } + break; + } + if ( !b ) + { + printk(XENLOG_INFO "Boot video device %02x:%02x.%u\n",= + bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); + pci_hide_device(bus, devfn); + } + } + } =20 switch ( vga_console_info.video_type ) { --- a/xen/include/xen/pci.h +++ b/xen/include/xen/pci.h @@ -103,6 +103,7 @@ int pci_add_device(u16 seg, u8 bus, u8 d int pci_remove_device(u16 seg, u8 bus, u8 devfn); int pci_ro_device(int seg, int bus, int devfn); void arch_pci_ro_device(int seg, int bdf); +int pci_hide_device(int bus, int devfn); struct pci_dev *pci_get_pdev(int seg, int bus, int devfn); struct pci_dev *pci_get_pdev_by_domain( struct domain *, int seg, int bus, int devfn); --=__PartD5E4C783.0__= Content-Type: text/plain; name="pci-disallow-assign.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="pci-disallow-assign.patch" PCI: don't allow guest assignment of devices used by Xen=0A=0AThis covers = the devices used for the console and the AMD IOMMU ones (as=0Awould be any = others that might get passed to pci_ro_device()).=0A=0ABoot video device = determination cloned from similar Linux logic.=0A=0ASigned-off-by: Jan = Beulich =0A---=0ANote that to apply cleanly, this has = to go on top of the serial console=0Aimprovement series posted earlier.=0A= =0A--- a/xen/arch/x86/mm.c=0A+++ b/xen/arch/x86/mm.c=0A@@ -311,9 +311,12 = @@ void __init arch_init_memory(void)=0A * Initialise our DOMID_XEN = domain.=0A * Any Xen-heap pages that we will allow to be mapped will = have=0A * their domain field set to dom_xen.=0A+ * Hidden PCI = devices will also be associated with this domain=0A+ * (but be = [partly] controlled by Dom0 nevertheless).=0A */=0A dom_xen =3D = domain_create(DOMID_XEN, DOMCRF_dummy, 0);=0A BUG_ON(IS_ERR(dom_xen));= =0A+ INIT_LIST_HEAD(&dom_xen->arch.pdev_list);=0A =0A /*=0A * = Initialise our DOMID_IO domain.=0A--- a/xen/drivers/char/ehci-dbgp.c=0A+++ = b/xen/drivers/char/ehci-dbgp.c=0A@@ -1364,6 +1364,8 @@ static void __init = ehci_dbgp_init_postir=0A init_timer(&dbgp->timer, ehci_dbgp_poll, = port, 0);=0A =0A ehci_dbgp_setup_postirq(dbgp);=0A+=0A+ pci_hide_dev= ice(dbgp->bus, PCI_DEVFN(dbgp->slot, dbgp->func));=0A }=0A =0A static int = ehci_dbgp_check_release(struct ehci_dbgp *dbgp)=0A--- a/xen/drivers/char/ns= 16550.c=0A+++ b/xen/drivers/char/ns16550.c=0A@@ -334,6 +334,10 @@ static = void __init ns16550_init_postirq(=0A }=0A =0A ns16550_setup_postirq= (uart);=0A+=0A+ if ( uart->bar || uart->ps_bdf_enable )=0A+ = pci_hide_device(uart->ps_bdf[0], PCI_DEVFN(uart->ps_bdf[1],=0A+ = uart->ps_bdf[2]));=0A }=0A =0A = static void ns16550_suspend(struct serial_port *port)=0A--- a/xen/drivers/p= assthrough/iommu.c=0A+++ b/xen/drivers/passthrough/iommu.c=0A@@ -208,7 = +208,7 @@ static int device_assigned(u16 seg, u8 b=0A pdev =3D = pci_get_pdev_by_domain(dom0, seg, bus, devfn);=0A spin_unlock(&pcidevs_= lock);=0A =0A- return pdev ? 0 : -1;=0A+ return pdev ? 0 : -EBUSY;=0A= }=0A =0A static int assign_device(struct domain *d, u16 seg, u8 bus, u8 = devfn)=0A@@ -614,7 +614,8 @@ int iommu_do_domctl(=0A bus =3D = (domctl->u.assign_device.machine_sbdf >> 8) & 0xff;=0A devfn =3D = domctl->u.assign_device.machine_sbdf & 0xff;=0A =0A- ret =3D = assign_device(d, seg, bus, devfn);=0A+ ret =3D device_assigned(seg, = bus, devfn) ?:=0A+ assign_device(d, seg, bus, devfn);=0A = if ( ret )=0A printk(XENLOG_G_ERR "XEN_DOMCTL_assign_device:= "=0A "assign %04x:%02x:%02x.%u to dom%d failed = (%d)\n",=0A--- a/xen/drivers/passthrough/pci.c=0A+++ b/xen/drivers/passthro= ugh/pci.c=0A@@ -208,6 +208,31 @@ static void free_pdev(struct pci_seg = *ps=0A xfree(pdev);=0A }=0A =0A+static void _pci_hide_device(struct = pci_dev *pdev)=0A+{=0A+ if ( pdev->domain )=0A+ return;=0A+ = pdev->domain =3D dom_xen;=0A+ list_add(&pdev->domain_list, &dom_xen->arc= h.pdev_list);=0A+}=0A+=0A+int __init pci_hide_device(int bus, int = devfn)=0A+{=0A+ struct pci_dev *pdev;=0A+ int rc =3D -ENOMEM;=0A+=0A+= spin_lock(&pcidevs_lock);=0A+ pdev =3D alloc_pdev(get_pseg(0), bus, = devfn);=0A+ if ( pdev )=0A+ {=0A+ _pci_hide_device(pdev);=0A+ = rc =3D 0;=0A+ }=0A+ spin_unlock(&pcidevs_lock);=0A+=0A+ = return rc;=0A+}=0A+=0A int __init pci_ro_device(int seg, int bus, int = devfn)=0A {=0A struct pci_seg *pseg =3D alloc_pseg(seg);=0A@@ -231,6 = +256,7 @@ int __init pci_ro_device(int seg, int bu=0A =0A __set_bit(PCI= _BDF2(bus, devfn), pseg->ro_map);=0A arch_pci_ro_device(seg, PCI_BDF2(b= us, devfn));=0A+ _pci_hide_device(pdev);=0A =0A return 0;=0A }=0A@@ = -718,9 +744,22 @@ static int __init _setup_dom0_pci_device=0A = if ( !pdev )=0A continue;=0A =0A- pdev->domain = =3D ctxt->d;=0A- list_add(&pdev->domain_list, &ctxt->d->arch.pde= v_list);=0A- ctxt->handler(pdev);=0A+ if ( !pdev->dom= ain )=0A+ {=0A+ pdev->domain =3D ctxt->d;=0A+ = list_add(&pdev->domain_list, &ctxt->d->arch.pdev_list);=0A+ = ctxt->handler(pdev);=0A+ }=0A+ else if ( = pdev->domain =3D=3D dom_xen )=0A+ {=0A+ = pdev->domain =3D ctxt->d;=0A+ ctxt->handler(pdev);=0A+ = pdev->domain =3D dom_xen;=0A+ }=0A+ else = if ( pdev->domain !=3D ctxt->d )=0A+ printk(XENLOG_WARNING = "Dom%d owning %04x:%02x:%02x.%u?\n",=0A+ pdev->domain= ->domain_id, pseg->nr, bus,=0A+ PCI_SLOT(devfn), = PCI_FUNC(devfn));=0A }=0A }=0A =0A--- a/xen/drivers/video/vga.c= =0A+++ b/xen/drivers/video/vga.c=0A@@ -9,6 +9,7 @@=0A #include = =0A #include =0A #include =0A+#include =0A = #include =0A =0A /* Filled in by arch boot code. */=0A@@ -106,6 = +107,61 @@ void __init vga_endboot(void)=0A =0A if ( !vgacon_keep )=0A = vga_puts =3D vga_noop_puts;=0A+ else=0A+ {=0A+ int = bus, devfn;=0A+=0A+ for ( bus =3D 0; bus < 256; ++bus )=0A+ = for ( devfn =3D 0; devfn < 256; ++devfn )=0A+ {=0A+ = const struct pci_dev *pdev;=0A+ u8 b =3D bus, df =3D = devfn, sb;=0A+=0A+ spin_lock(&pcidevs_lock);=0A+ = pdev =3D pci_get_pdev(0, bus, devfn);=0A+ spin_unlock(&p= cidevs_lock);=0A+=0A+ if ( !pdev ||=0A+ = pci_conf_read16(0, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),=0A+ = PCI_CLASS_DEVICE) !=3D 0x0300 ||=0A+ = !(pci_conf_read16(0, bus, PCI_SLOT(devfn),=0A+ = PCI_FUNC(devfn), PCI_COMMAND) &=0A+ = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) )=0A+ = continue;=0A+=0A+ while ( b )=0A+ {=0A+ = switch ( find_upstream_bridge(0, &b, &df, &sb) )=0A+ = {=0A+ case 0:=0A+ b = =3D 0;=0A+ break;=0A+ case = 1:=0A+ switch ( pci_conf_read8(0, b, PCI_SLOT(df),= =0A+ PCI_FUNC(df),=0A+ = PCI_HEADER_TYPE) )=0A+ = {=0A+ case PCI_HEADER_TYPE_BRIDGE:=0A+ = case PCI_HEADER_TYPE_CARDBUS:=0A+ = if ( pci_conf_read16(0, b, PCI_SLOT(df),=0A+ = PCI_FUNC(df),=0A+ = PCI_BRIDGE_CONTROL) &=0A+ = PCI_BRIDGE_CTL_VGA )=0A+ continue;=0A+ = break;=0A+ }=0A+ = break;=0A+ }=0A+ = break;=0A+ }=0A+ if ( !b )=0A+ = {=0A+ printk(XENLOG_INFO "Boot video device %02x:%02x.%= u\n",=0A+ bus, PCI_SLOT(devfn), PCI_FUNC(devfn));= =0A+ pci_hide_device(bus, devfn);=0A+ = }=0A+ }=0A+ }=0A =0A switch ( vga_console_info.video_type= )=0A {=0A--- a/xen/include/xen/pci.h=0A+++ b/xen/include/xen/pci.h=0A@= @ -103,6 +103,7 @@ int pci_add_device(u16 seg, u8 bus, u8 d=0A int = pci_remove_device(u16 seg, u8 bus, u8 devfn);=0A int pci_ro_device(int = seg, int bus, int devfn);=0A void arch_pci_ro_device(int seg, int = bdf);=0A+int pci_hide_device(int bus, int devfn);=0A struct pci_dev = *pci_get_pdev(int seg, int bus, int devfn);=0A struct pci_dev *pci_get_pdev= _by_domain(=0A struct domain *, int seg, int bus, int devfn);=0A --=__PartD5E4C783.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 --=__PartD5E4C783.0__=--