From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Michael S. Tsirkin" Subject: Re: [PATCH] dev-assignment: handle device with incorrect PCIe Cap structure size Date: Wed, 27 Jul 2011 10:48:00 +0300 Message-ID: <20110727074800.GA15762@redhat.com> References: <20110726203722.39565.29092.stgit@dddsys0.bos.redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: kvm@vger.kernel.org, alex.williamson@redhat.com To: Donald Dutile Return-path: Received: from mx1.redhat.com ([209.132.183.28]:42378 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752553Ab1G0Hr3 (ORCPT ); Wed, 27 Jul 2011 03:47:29 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p6R7lTcw018314 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 27 Jul 2011 03:47:29 -0400 Content-Disposition: inline In-Reply-To: <20110726203722.39565.29092.stgit@dddsys0.bos.redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Tue, Jul 26, 2011 at 06:08:09PM -0400, Donald Dutile wrote: > The bcm5761 provides a PCIe Cap structure (capid=0x10) > that is invalid, providing one that is 8 bytes shorter > than the v2 PCIe spec defines. > This leads to a memory corruption when mapped for device-assigment. > > Add a check in assigned_device_pci_cap_init() to correct > this hw error for this device, and try to catch other ones > and print warnings if they exists. > > Signed-off-by: Donald Dutile > cc: Alex Williamson > cc: Michael S. Tsirking Acked-by: Michael S. Tsirkin > --- > > hw/device-assignment.c | 29 ++++++++++++++++++++++++----- > 1 files changed, 24 insertions(+), 5 deletions(-) > > diff --git a/hw/device-assignment.c b/hw/device-assignment.c > index 36ad6b0..e073840 100644 > --- a/hw/device-assignment.c > +++ b/hw/device-assignment.c > @@ -1419,18 +1419,37 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev) > } > > if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) { > - uint8_t version; > + uint8_t version, size; > uint16_t type, devctl, lnkcap, lnksta; > uint32_t devcap; > - int size = 0x3c; /* version 2 size */ > > version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS); > version &= PCI_EXP_FLAGS_VERS; > if (version == 1) { > size = 0x14; > - } else if (version > 2) { > - fprintf(stderr, "Unsupported PCI express capability version %d\n", > - version); > + } else if (version == 2) { > + /* > + * Check for non-std size, accept reduced size to 0x34, > + * which is what bcm5761 implemented, violating the > + * PCIe v3.0 spec that regs should exist and be read as 0, > + * not optionally provided and shorten the struct size. > + */ > + size = MIN(0x3c, PCI_CONFIG_SPACE_SIZE - pos); > + if (size < 0x34) { > + fprintf(stderr, > + "%s: Invalid size PCIe cap-id 0x%x \n", > + __func__, PCI_CAP_ID_EXP); > + return -EINVAL; > + } else if (size != 0x3c) { > + fprintf(stderr, > + "WARNING, %s: PCIe cap-id 0x%x has " > + "non-standard size 0x%x; std size should be 0x3c \n", > + __func__, PCI_CAP_ID_EXP, size); > + } > + } else { > + fprintf(stderr, > + "%s: Unsupported PCI express capability version %d\n", > + __func__, version); > return -EINVAL; > } >